Share via


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

Question

Saturday, March 3, 2018 7:44 PM

i am facing problem when run following code . 

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

 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DataIN += serialPort1.ReadExisting();
            DataIN = DataIN.Replace("*", "0");string SampleID = DataIN.Substring(11, 12);

analyzerIDTextBox.Text = SampleID.ToString();}

kindly advise us how to solve this problem 

All replies (5)

Tuesday, March 6, 2018 8:05 AM âś…Answered

Hello saqsaqPK,

 ***"Control 'TextBox' accessed from a thread other than the thread it was created on", *******As exception indicates, TextBox control only access in UI thread (The thread textbox was created on), but the DataReceived event is triggered by another thread. So you need to use invoke method because this will dispatch executing code to UI thread to run.

Best regards,

Neil Hu****

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Saturday, March 3, 2018 7:49 PM

Basically the serialPort1_DataReceived method is not executing on the caller or UI thread.

Please see the following post for the solution of this issue:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/6ded4abf-e063-4514-8b91-97274c074912/crossthread-operation-not-valid-control-richtextbox1-accessed-from-a-thread-other-than-the?forum=vclanguage

and this post:

https://stackoverflow.com/a/142069/1875256

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

Blog | LinkedIn | Stack Overflow | Facebook


Monday, March 5, 2018 7:08 AM

Hello saqasqPK,

Try the blow code that Ehasn suggested.

         DataIN += serialPort1.ReadExisting();
         DataIN = DataIN.Replace("*", "0");

          string SampleID = DataIN.Substring(11, 12);

          analyzerIDTextBox.Invoke(new Action(()=> analyzerIDTextBox.Text= SampleID.ToString()));

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, March 5, 2018 1:53 PM

Thanks for your replay . its working excellent . 

can you tell me  why do we use "Invoke " in what condition we need  for invoke ? . searched google but confused .

Regards.


Monday, March 5, 2018 2:08 PM

An application can have several 'threads' or parallel processes occurring at the same time.

There will always be a main 'GUI' thread - this is the only thread that can update user-interface controls.

In a very simple application this main thread will be the only thread and you don't have to worry about it. But sometimes you may want some bit of processing to occur asynchronously. Depending on the logic you use, this asynchronous code may or may not be in another thread but you can always explicitly ensure code runs on another thread.

In your case above, as someone has already pointed out, the serialport DataReceived event is firing in another thread.

When you have code executing in another thread, this code cannot directly update user-interface controls. This is why you use Invoke. This basically tells the system "I want to perform this update action on the TextBox's own thread (the main UI thread)".