Click here to Skip to main content
13,148,573 members (56,456 online)
Rate this:
Please Sign up or sign in to vote.
Here is code snnipet where my code terminates by throwing an exception..
public void OnDataReceived(IAsyncResult asyn)
                  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...              
              catch (ObjectDisposedException)
                  System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
              catch (SocketException se)

How do i solve this 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 27-Oct-12 18:18pm
Updated 28-Oct-12 0:23am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Change to :
StateObject so = (StateObject)asyn.AsyncState;
Socket s = so.workSocket;
int iRx = s.EndReceive(asyn);
if (iRx > 0) {
Sharath2790 28-Oct-12 1:53am
What is a StateObject??? is there any class called StateObject...
Kuthuparakkal 28-Oct-12 1:55am
Follow :
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
<a href= "">Socket</a>
Sharath2790 28-Oct-12 23:47pm
Thank you!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100