Click here to Skip to main content
15,884,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is code snnipet where my code terminates by throwing an exception..
C#
public void OnDataReceived(IAsyncResult asyn)
          {
              try
              {
                  SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
                  int iRx = theSockId.thisSocket.EndReceive(asyn);
                  char[] chars = new char[iRx + 1];
                  System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                  int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                  System.String szData = new System.String(chars);
                  richTextBox1.Text=richTextBox1.Text + szData[0];
                  //IT IS WHERE PROGRAM TERMINATES...              
                  WaitForData();
                 
              }
              catch (ObjectDisposedException)
              {
                  System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
              }
              catch (SocketException se)
              {
                  MessageBox.Show(se.Message,"2");
             }
          }

How do i solve this exception...
Exception:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
The program '[3908] Display screen.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[3908] Display screen.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
Posted
Updated 28-Oct-12 0:23am
v2

Change to :
C#
StateObject so = (StateObject)asyn.AsyncState;
Socket s = so.workSocket;
int iRx = s.EndReceive(asyn);
if (iRx > 0) {
......
 
Share this answer
 
Comments
Sharath2790 28-Oct-12 1:53am    
What is a StateObject??? is there any class called StateObject...
Kuthuparakkal 28-Oct-12 1:55am    
Follow : http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx
Sharath2790 28-Oct-12 6:36am    
Sorry!! That doesn't work either... Please Suggest me a simple example where one windows forms application sends data and another application receives and displays it...
Kuthuparakkal 28-Oct-12 10:45am    
Try:
<a href= "http://csharp.net-informations.com/communications/csharp-socket-programming.htm">Socket</a>
Sharath2790 28-Oct-12 23:47pm    
Thank you!!
When the update of the textbox is made from an other thread, make the update through a delegate.

Sample code :

SynchronizationContext.Current.Send((state) =>
    {    
        richTextBox1.Text=richTextBox1.Text + szData[0];
    } 
, null);


See google for further reading on InvokeRequired and BeginInvoke or SynchronizationContext.Current

Piet
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900