Click here to Skip to main content
11,715,160 members (63,887 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WCF C#4.0 VS2010 Sockets
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
Edited 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 at 28-Oct-12 1:53am
What is a StateObject??? is there any class called StateObject...
Kuthuparakkal at 28-Oct-12 1:55am
Follow :
Sharath2790 at 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 at 28-Oct-12 10:45am
<a href= "">Socket</a>
Sharath2790 at 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
0 Sergey Alexandrovich Kryukov 495
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2015
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