By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Cross-thread operation not valid:

Raventara
P: 11
Hi peoples....

This is a pain in the ass:
Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.

I am trying to update the text in the textbox when data is recieved on the new SerialPort control.

So I added the following:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
  2.         Dim tmp As Integer
  3.         Dim chr As String
  4.  
  5.         tmp = Me.SerialPort1.ReadChar
  6.         chr = System.Convert.ToChar(tmp)
  7.         Me.TextBox1.Text = Me.TextBox1.Text & chr
  8. End Sub
  9.  
Which yields the previous error.
So I add the things described in: How to: Make Thread-Safe Calls to Windows Forms Controls from the MSDN but I must have done something wrong:
Expand|Select|Wrap|Line Numbers
  1.     Delegate Sub addtotextboxPROX(ByVal [text] As String)
  2.  
  3.     Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
  4.         Dim tmp As Integer
  5.         Dim chr As String
  6.  
  7.         tmp = Me.SerialPort1.ReadChar
  8.         chr = System.Convert.ToChar(tmp)
  9.         Me.addtotextbox(chr)
  10.  
  11.  
  12.     End Sub
  13.     Private Sub addtotextbox(ByVal txt As String)
  14.         If Me.TextBox1.InvokeRequired Then
  15.             Dim d As New addtotextboxPROX(AddressOf addtotextbox)
  16.             Me.Invoke(d, New Object() {[Text]})
  17.         Else
  18.             Me.TextBox1.Text = Me.TextBox1.Text & [Text]
  19.         End If
  20.  
  21.     End Sub
  22.  
And now I get the name of the form "Form1" appended to the text box!?!?!

Can somebody please explain to me how all this stuff is supposed to work coz I can't make head nor tails of it. I've never dealt with threads before in VB but clearly it's running the code for the SerialPort control in another thread.

Please please please help

Rave-n
Sep 24 '07 #1
Share this Question
Share on Google+
1 Reply


Raventara
P: 11
OK - the following lines were wrong:
Expand|Select|Wrap|Line Numbers
  1. Me.Invoke(d, New Object() {[Text]})
  2.         Else
  3.             Me.TextBox1.Text = Me.TextBox1.Text & [Text] 
Should have read:
Expand|Select|Wrap|Line Numbers
  1. Me.Invoke(d, New Object() {txt})
  2.         Else
  3.             Me.TextBox1.Text = Me.TextBox1.Text & txt 
Just looking at things from the wrong point of view. It works now, but it would still be nice if someone could explain it to me :S

Cheers,

Rave-n
Sep 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.