locked
Invalid Operation Exception was unhandled RRS feed

  • Question

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

    I am not sure why it is saying that or how to fix it....

     private void mySp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                // If the com port has been closed, do nothing
                if (!mySp.IsOpen) return;

                // This method will be called when there is data waiting in the port's buffer

                // Determain which mode (string or binary) the user is in
              
               
                    // Read all the data waiting in the buffer
                    get2.Text = get2.Text + mySp.ReadLine();

                    // Display the text to the user in the terminal
                   
              
               
            }

    Thursday, June 17, 2010 8:03 AM

Answers

  • A: The SerialPort class raises events on a separate thread than the main form was create on.  Windows Forms controls must be modified only on their original thread.  Thankfully there is an easy way to do this.  Each Windows control has a "Invoke" method which will run code on the control's original thread.  So to put the recently received data into a text box (txtLog), this would do it:   txtLog.Invoke(new EventHandler(delegate { txtLog.Text += comport.ReadExisting(); });   You can see this more in action in the "Log" event of "Terminal.cs" my sample code project, SerialPortTerminal.zip .
    • Marked as answer by Riten900 Thursday, June 17, 2010 8:32 PM
    Thursday, June 17, 2010 8:09 PM

All replies

  • .NET Framework doesn't allow thread-unsafe calls. Here get2 may have been declared in a form and accessed via a thread.

    Check this for learning how to make thread-safe calls.

    http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx


    Regards, Chethan Kamath. "Remember that if the world didn't suck, we'd all fall off."
    Thursday, June 17, 2010 8:22 AM
  • ok what I don't get is why I am getting a Cross-thread operation in the 1st place, because I am not calling any threads...

    never in my call do I even use the word thread other then  using System.Threading;

     

    ok I read that link  Chethan Kamath ,  and I tried this.get2.Text = this.get2.Text + mySp.ReadLine();

    but that did not work?

    and like I said I am not calling any thread ...

    so I am not too sure what to do

     

     and I changed private void mySp_DataReceived(object sender, SerialDataReceivedEventArgs e) to

      void mySp_DataReceived(object sender, SerialDataReceivedEventArgs e)

    but it did not help

    Thursday, June 17, 2010 12:21 PM
  • The documentation says that the DataReceived event happens on a different thread:

    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=VS.80).aspx

    It also makes sense, because otherwise your entire application would hang while it's waiting for data...


    Mark the best replies as answers!
    • Proposed as answer by Louis.fr Thursday, June 17, 2010 8:08 PM
    Thursday, June 17, 2010 12:38 PM
  • It seems you are doing an asynchronous operation (from the name of your event handler). This may be getting called on a separate thread (though you don't specifically create one).
    http://blog.voidnish.com
    Thursday, June 17, 2010 12:38 PM
  • so how do I fix it?
    Thursday, June 17, 2010 8:08 PM
  • A: The SerialPort class raises events on a separate thread than the main form was create on.  Windows Forms controls must be modified only on their original thread.  Thankfully there is an easy way to do this.  Each Windows control has a "Invoke" method which will run code on the control's original thread.  So to put the recently received data into a text box (txtLog), this would do it:   txtLog.Invoke(new EventHandler(delegate { txtLog.Text += comport.ReadExisting(); });   You can see this more in action in the "Log" event of "Terminal.cs" my sample code project, SerialPortTerminal.zip .
    • Marked as answer by Riten900 Thursday, June 17, 2010 8:32 PM
    Thursday, June 17, 2010 8:09 PM
  • I would prefer BeginInvoke over Invoke so the calling thread won't block.
    http://blog.voidnish.com
    Thursday, June 17, 2010 8:15 PM
  • what do you mean ?
    Thursday, June 17, 2010 8:34 PM
  • If you use Control.Invoke, your code will wait till the call returns, and this can be a nuisance if the main thread is currently busy. BeginInvoke is asynchronous. So you won't run into this situation.
    http://blog.voidnish.com
    Thursday, June 17, 2010 8:38 PM
  • My Code is some thing like this

    public

     

    void OnClientConnect(IAsyncResult asyn)

    {

     

    try

    {

    m_workerSocket[m_clientCount] = m_mainSocket.EndAccept(asyn); ' I am geting a error here if i reset my Socket' "Exception : Invalid Opeartion Exception Was Unhandeled" Description : EndAccept can Only be called once foro wach asynchronous Operation;

    WaitForData(m_workerSocket[m_clientCount]);

    ++m_clientCount;

     

     

    string str = String.Format("Client # {0} connected", m_clientCount);

     

    can any one help ?

    Friday, August 6, 2010 4:51 AM