473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cross-thread operation not valid:

Raventara
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
1 1891
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

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

Similar topics

0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
by: rollasoc | last post by:
Hi, Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app). Did a bit of testing. Loaded a previously saved file. A gray box appeared with the text and buttons all white...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
1
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
6
by: Bart Van der Donck | last post by:
Hello, I'm presenting my new library 'AJAX Cross Domain' - a javascript extension that allows to perform cross-domain AJAX requests. http://www.ajax-cross-domain.com/ Any comments or...
6
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.