Click here to Skip to main content
Rate this: bad
good
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)
          {
              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 27-Oct-12 19:18pm
Edited 28-Oct-12 1:23am
v2
Rate this: bad
good
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) {
......
  Permalink  
Comments
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 : http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx
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
   
Try:
<a href= "http://csharp.net-informations.com/communications/csharp-socket-programming.htm">Socket</a>
Sharath2790 at 28-Oct-12 23:47pm
   
Thank you!!
Rate this: bad
good
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
 
Piet
  Permalink  
v2

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 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2014
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