|
This aint Java
|
|
|
|
|
hmm i use #develop with C# xD
|
|
|
|
|
I have been thinking about how it would make sense to write code like this.
After all, you wouldn't have written it like this if you had no reason to do so.
I think I figured it out - did you think that return value; only sets the value that will be returned, instead of immediately* returning that value? It all makes sense then - as long as they are not equal, you would keep the return value on false ; and when they are equal you'd set the return value to true and exit the method.
The problem with that is, of course, that return doesn't "set the value that will be returned", it immediately* exits the method (and it also returns the specified value, of course)
*: except in the presence of try /finally
|
|
|
|
|
After the for loop
you should return a value,
because perhaps the for loop is not executed
|
|
|
|
|
Hi All
I am using TcpClient to read from a socket and I set the property ReceiveTimeout to a value let's say 3 seconds.
As per documentation, after the read fails because of a Timeout, an IOexception is thrown.
After that point, each time I call ReadLine the exception is systematically thrown again and again.
What to do to reset the Timeout value or to renable the reading from a socket with no worry?
I reset a new value for ReceiveTimeout but it doesn't work.
Do you know what to do?
|
|
|
|
|
Don't know if this will help you or not.....
I have a UDP client based application. We were having troubles reconnecting the computers when one of the network cards would go down for some reason. What I ended up doing was checking for exceptions, and depending on the exceptions, I would then try closing/reopening the socket.
Maybe this is what you need to do.
You should also probably test for the case I had which was to have the apps communicating, and then disable the network adapter and reenable it.
|
|
|
|
|
i don't think the exception is thrown because of the ReceiveTimeout.. if nothings availible to read the buffer would be empty and that would may by throw an exception..
try to check before reading using the property
<br />
if (client.DataAvailable > 0)<br />
{<br />
...<br />
string line = client.ReadLine();<br />
...<br />
}<br />
|
|
|
|
|
hi Freakyit
thank you very much for this snippet..I didn't know about it
Mmstn
|
|
|
|
|
A timeout closes the connection. You have to re-establish the connection. Make sure your TcpClient has keepalive set to true, and make sure the server isn't timing out on you. Also, a higher timeout value can help unless there is a major issue with the server timing out, in which case you will just wait longer for the exception.
|
|
|
|
|
Thank you very mcuh! I didn't know that! It helps!
mnstn
|
|
|
|
|
I have used
list<> data type to store emp id. As
List<int> EmpIds = new List<int>() ;
But it takes duplicate entries.
Which data structure I should use to have collection of unique ids.
|
|
|
|
|
Have a look at HashSet<T> ; it requires .NET 3.5+
|
|
|
|
|
System.Collections.Generic.HashSet<int>
|
|
|
|
|
One slight caveat with HashSet: the data in a HashSet is not sorted. You cannot guarantee what order the entries will be in when you enumerate over them. If that doesn't matter to you, HashSet is great. If the order matters, then you could use SortedSet (which would sort them in numeric sequence). If you need them in some other order (e.g. the order in which they were added) you may be better off building your own UniqueList (which isn't hard to do, just an IList wrapper around List to check if the entry is already there when you add it).
|
|
|
|
|
A simple code to export data to excel
public partial class Form1 : Form
{
Microsoft.Office.Interop.Excel.Application _objAppln;
Workbook _objWorkBook;
Workbooks _objWorkBooks;
Worksheet _objWorkSheet;
public Form1()
{
InitializeComponent();
InitializeExcelObjectModel();
}
~Form1()
{
DisposeExcelObjects();
}
void SaveExcel()
{
_objAppln.DisplayAlerts = false;
_objWorkBook.SaveAs("C:\\tmp.xls",
XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_objWorkBook.Close(true, "C:\\tmp.xls", false);
_objAppln.DisplayAlerts = true;
}
private void InitializeExcelObjectModel()
{
_objAppln = new Microsoft.Office.Interop.Excel.Application();
if (_objAppln != null)
{
_objWorkBooks = _objAppln.Workbooks;
_objWorkBook = _objWorkBooks.Add(Type.Missing);
_objWorkSheet = (Worksheet)_objAppln.ActiveSheet;
}
}
public void DisposeExcelObjects()
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_objAppln);
_objWorkSheet = null; _objWorkBooks = null; _objWorkBooks = null; _objAppln = null;
}
private void button1_Click(object sender, EventArgs e)
{
Range objRange=null;
string cell1 = string.Empty, cell2 = string.Empty;
string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T" };
for (int row = 1; row < 1000; row++)
{
for (int column = 0; column < 20; column++)
{
cell1 = chars[column] + row.ToString();
objRange = _objWorkSheet.get_Range(cell1, cell1);
objRange.Value2 = cell1;
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
objRange = null;
SaveExcel();
}
}
Step 1: Add the above code to a solution and add reference for Microsoft.Office.Interop.Excel.dll
Step 2: Run the application and Execute the code in button1_Click.
Step 3: While the processing is on, open any other excel file and just click on cells here and there.
COM exception occurs. Anything I am doing wrong above?
How to resolve?
Gurpreet
|
|
|
|
|
hi,
what is the exactly com exception ??
|
|
|
|
|
Exception from HRESULT: 0x800AC472
Gurpreet
|
|
|
|
|
Made some changes in the Button_Click event and could not reproduce the issue.
//Range objRange = null;
string cell1 = string.Empty, cell2 = string.Empty;
string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T" };
for (int row = 1; row < 1000; row++)
{
for (int column = 0; column < 20; column++)
{
cell1 = chars[column] + row.ToString();
//objRange = _objWorkSheet.get_Range(cell1, cell1);
_objWorkSheet.get_Range(cell1, cell1).Value2 = cell1;
}
}
//System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
//objRange = null;
SaveExcel();
|
|
|
|
|
I tried...
but I still get the same error (exception from HRESULT: 0x800AC472)
While the code in for loop is executing, you need to open any other excel file and click on randon cells.
Gurpreet
|
|
|
|
|
I did but still can't reproduce. Using Excel 2010.
|
|
|
|
|
Oh! But I am getting this evevytime i try to do so
My environment is:
Visual Studio 2010
Excel - 2008
Here is the detailed error:
System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.set_Value2(Object )
at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\kaurrgur\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 77
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Gurpreet
|
|
|
|
|
hi it might work setting the value into the cell by not using the objRange.Value2 property ?!? this is only a suggestion.
|
|
|
|
|
I tried like below but it does not solve the problem
_objWorkSheet.Cells[row, column] = cell1;
Gurpreet
|
|
|
|
|
try setting the .IgnoreRemoteRequests to true right after you open your instance of excel, and don't forget to set it back to false when done as this affects the behavior of your entire environment.
|
|
|
|
|
Finally some solution which works
But still while my code is executing, I cannot open any other excel file... which is not good
If I look at the behaviour of Visual Studio (when I export my work list to excel), it works perfectly fine... no error and I can open the other excel too...
So whats wrong with my code... Is there any other better way to export?
Gurpreet
|
|
|
|
|