or nakash

or nakash

  • NA
  • 23
  • 0

Asynchronous Sockets ERROR

Feb 13 2010 1:19 PM
I am getting an error in this code:

        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
                //end receive...
                int iRx = 0;
                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 += szData;
                WaitForData(m_socWorker);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

THE UNDERLINE CODE GIVES ME THIS ERROR:

Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

And one more question, how to do that when I type the string "pw1313" it executes a Console.Beep(); event?

Answers (6)