none
cross-thread operation not valid:control 'textBox' accessed from a thread other than the thread it was created on in c++

    Question

  • Hi 

    please help me,

    I use textBox1,2 and 3, but i got error when i debug. it is ok when i choose without debug.

    I got error as below:

    "cross-thread operation not valid:control 'textBox' accessed from a thread other than the thread it was created on "

    i don't know how to solve it

    Thanks in advance

    Thursday, February 09, 2012 3:01 AM

All replies

  • Hi Mickeydora,

    I have created a CLR Windows application, and added textBox1, textBox2, and textBox3 onto the forum. But I was not able to reproduce this issue on my side. Could you please provide the steps by which you received this error?

    As the message suggests, the error is caused by accessing the control from another thread which is not the one creating it. So please try to eliminate the illegal codes which leads to the access. To do this, I suggest you try to find the related code smippet, and then do some modification to let the codes run in the normal way. Or you can post the related codes here so that we can come up with a solution together.

    By the way, the following links have a similar topic with this thread. Please refer to them for more useful information:
    http://social.msdn.microsoft.com/Forums/en-CA/vcgeneral/thread/a284b029-88d5-4374-b022-595cbd0138a3.
    http://social.msdn.microsoft.com/Forums/en/clr/thread/e711c0cb-c8e0-43bf-bc36-95fe45cb6273.

    Best regards,
    Helen Zhao


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 16, 2012 2:01 AM
  • hi,

    I write one of the refer as below:

    <p></p><p></p><p>void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
    	if (richTextBox1->InvokeRequired) {
    		richTextBox1->Invoke(
    			gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived),
    			gcnew array<System::Object^> { sender, e }
    		);
    	}
    	else {
    		richTextBox1->Text += serialPort1->ReadLine();
    	}
    }</p><p>there is no complie error but I connect with my device. I got error as below:</p><p></p><p>"<strong style="font-family:inherit">An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll</strong></p><br/><br/><strong>Additional information: Index was outside the bounds of the array.</strong>"
    

    I don't why show this error. Please help me!!

    Thanks in advance

    Monday, February 20, 2012 9:38 AM
  • I have created a CLR Windows Application.

    And then I draged a RichTextBox and a SerialPort onto Form1.

    Finally, I copied the codes you provided.

    I was not able to reproduce the issue. 

    could you please explain the steps pf reproducing the error in detail? 

    Wednesday, February 22, 2012 7:58 AM
  • HI,

    I write as below:

    void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
    	if (richTextBox1->InvokeRequired) {
    		richTextBox1->Invoke(
    			gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived),
    			gcnew array<System::Object^> { sender, e }
    		);
    	}
    	else {
    		richTextBox1->Text += serialPort1->ReadLine();
    	}

    I got error when i debug as following:

    An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll


    Additional information: Index was outside the bounds of the array.

    I don't know this error where is come from.

    Thanks in advance

    Thursday, March 08, 2012 8:57 AM