PC Review


Reply
Thread Tools Rate Thread

Cross Threading error????

 
 
Terry Olsen
Guest
Posts: n/a
 
      20th Nov 2004
I run this code:
Private Sub p_recv(ByVal sender As Object, ByVal e As
SerialReceivedEventArgs) Handles p.ReceivedEvent
txtRecv.Text += p.ReadExisting
End Sub

I get this error:

--------------------ERROR TEXT--------------------
Illegal cross-thread operation: Control 'txtRecv' accessed from a thread
other than the thread it was created on.
Stack trace where the illegal operation occurred was:

at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_WindowText()
at System.Windows.Forms.TextBoxBase.get_WindowText()
at System.Windows.Forms.Control.get_Text()
at System.Windows.Forms.TextBoxBase.get_Text()
at System.Windows.Forms.TextBox.get_Text()
at ComPortTest.Form1.p_recv(Object, SerialReceivedEventArgs)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object,
SerialReceivedEventArgs)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object)
at System.Threading.ExecutionContext.Run(ExecutionContext, ContextCallback,
Object, StackCrawlMark&)
at System.Threading._ThreadPoolWaitCallback.WaitCallback(Object)
---------------END ERROR TEXT-----------------------

Evidently, the function I'm calling creates a thread. So how can I write to
the textbox from the function without getting this error?


 
Reply With Quote
 
 
 
 
Ray Cassick \(Home\)
Guest
Posts: n/a
 
      20th Nov 2004
Windows forms controls are not thread safe. You cannot update them from a
different thread then they are running on. Take a look at this article to
get an idea of how to do it:

http://www.codenotes.com/articles/ar...?articleID=515

"Terry Olsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I run this code:
> Private Sub p_recv(ByVal sender As Object, ByVal e As
> SerialReceivedEventArgs) Handles p.ReceivedEvent
> txtRecv.Text += p.ReadExisting
> End Sub
>
> I get this error:
>
> --------------------ERROR TEXT--------------------
> Illegal cross-thread operation: Control 'txtRecv' accessed from a thread
> other than the thread it was created on.
> Stack trace where the illegal operation occurred was:
>
> at System.Windows.Forms.Control.get_Handle()
> at System.Windows.Forms.Control.get_WindowText()
> at System.Windows.Forms.TextBoxBase.get_WindowText()
> at System.Windows.Forms.Control.get_Text()
> at System.Windows.Forms.TextBoxBase.get_Text()
> at System.Windows.Forms.TextBox.get_Text()
> at ComPortTest.Form1.p_recv(Object, SerialReceivedEventArgs)
> at System.IO.Ports.SerialPort.CatchReceivedEvents(Object,
> SerialReceivedEventArgs)
> at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object)
> at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object)
> at System.Threading.ExecutionContext.Run(ExecutionContext,

ContextCallback,
> Object, StackCrawlMark&)
> at System.Threading._ThreadPoolWaitCallback.WaitCallback(Object)
> ---------------END ERROR TEXT-----------------------
>
> Evidently, the function I'm calling creates a thread. So how can I write

to
> the textbox from the function without getting this error?
>
>



 
Reply With Quote
 
Terry Olsen
Guest
Posts: n/a
 
      20th Nov 2004
Hey thanks! That helped. Here's the code that works.

Private Delegate Sub TextBoxInvoker(ByVal txt As String)

Private Sub p_recv(ByVal sender As Object, ByVal e As
SerialReceivedEventArgs) Handles p.ReceivedEvent
Dim txt As String = p.ReadExisting
txtRecv.Invoke(CType(AddressOf UpdateTextBox, TextBoxInvoker), txt)
End Sub

Private Sub UpdateTextBox(ByVal txt As String)
txtRecv.Text += txt
End Sub


"Ray Cassick (Home)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Windows forms controls are not thread safe. You cannot update them from a
> different thread then they are running on. Take a look at this article to
> get an idea of how to do it:
>
> http://www.codenotes.com/articles/ar...?articleID=515
>



 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      21st Nov 2004
On 2004-11-20, Terry Olsen <(E-Mail Removed)> wrote:
> Hey thanks! That helped. Here's the code that works.
>
> Private Delegate Sub TextBoxInvoker(ByVal txt As String)
>
> Private Sub p_recv(ByVal sender As Object, ByVal e As
> SerialReceivedEventArgs) Handles p.ReceivedEvent
> Dim txt As String = p.ReadExisting
> txtRecv.Invoke(CType(AddressOf UpdateTextBox, TextBoxInvoker), txt)
> End Sub
>
> Private Sub UpdateTextBox(ByVal txt As String)
> txtRecv.Text += txt
> End Sub
>


That's one way, or you can just invoke your method again...

Private Sub p_recv (ByVal sender As Object, ByVal e As
SerialRecievedEventArgs) Handles p.ReceivedEvent

If txtRecv.InvokeRequired Then
txtRecv.Invoke (AddressOf p_recv, new Object() {sender, e})
Else
txtRecv.Text += p.ReadExisting
End If
End Sub


--
Tom Shelton [MVP]
 
Reply With Quote
 
Terry Olsen
Guest
Posts: n/a
 
      21st Nov 2004
I also just discovered the "TextBox.AppendText" method. More elegant than
TextBox.Text += "NewText"

"Tom Shelton" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> On 2004-11-20, Terry Olsen <(E-Mail Removed)> wrote:
>> Hey thanks! That helped. Here's the code that works.
>>
>> Private Delegate Sub TextBoxInvoker(ByVal txt As String)
>>
>> Private Sub p_recv(ByVal sender As Object, ByVal e As
>> SerialReceivedEventArgs) Handles p.ReceivedEvent
>> Dim txt As String = p.ReadExisting
>> txtRecv.Invoke(CType(AddressOf UpdateTextBox, TextBoxInvoker), txt)
>> End Sub
>>
>> Private Sub UpdateTextBox(ByVal txt As String)
>> txtRecv.Text += txt
>> End Sub
>>

>
> That's one way, or you can just invoke your method again...
>
> Private Sub p_recv (ByVal sender As Object, ByVal e As
> SerialRecievedEventArgs) Handles p.ReceivedEvent
>
> If txtRecv.InvokeRequired Then
> txtRecv.Invoke (AddressOf p_recv, new Object() {sender, e})
> Else
> txtRecv.Text += p.ReadExisting
> End If
> End Sub
>
>
> --
> Tom Shelton [MVP]



 
Reply With Quote
 
Terry Olsen
Guest
Posts: n/a
 
      27th Nov 2004
Hello Tom. I was trying your method in the following code, but I get this
error in design time;
"'AddressOf' expression cannot be converted to 'System.Delegate' because
'System.Delegate' is not a delegate type"
Any Ideas??? Also, would this even work? It seems as if it might be trying
to read data into myBuffer twice (2nd time when the invoke occurrs).
Thanks!

Private Sub myListener_DataArrival() Handles myListener.DataArrival
Debug.Print("DataArrival Event")
Dim myBuffer() As Byte = New Byte() {}
myListener.ReadData(myBuffer)
If txtReceive.InvokeRequired Then
txtReceive.Invoke(AddressOf myListener_DataArrival) 'Error
occurrs here.
Else
txtReceive.AppendText(System.Text.Encoding.ASCII.GetString(myBuffer))
End If
End Sub



"Tom Shelton" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> On 2004-11-20, Terry Olsen <(E-Mail Removed)> wrote:
>> Hey thanks! That helped. Here's the code that works.
>>
>> Private Delegate Sub TextBoxInvoker(ByVal txt As String)
>>
>> Private Sub p_recv(ByVal sender As Object, ByVal e As
>> SerialReceivedEventArgs) Handles p.ReceivedEvent
>> Dim txt As String = p.ReadExisting
>> txtRecv.Invoke(CType(AddressOf UpdateTextBox, TextBoxInvoker), txt)
>> End Sub
>>
>> Private Sub UpdateTextBox(ByVal txt As String)
>> txtRecv.Text += txt
>> End Sub
>>

>
> That's one way, or you can just invoke your method again...
>
> Private Sub p_recv (ByVal sender As Object, ByVal e As
> SerialRecievedEventArgs) Handles p.ReceivedEvent
>
> If txtRecv.InvokeRequired Then
> txtRecv.Invoke (AddressOf p_recv, new Object() {sender, e})
> Else
> txtRecv.Text += p.ReadExisting
> End If
> End Sub
>
>
> --
> Tom Shelton [MVP]



 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      27th Nov 2004
On 2004-11-27, Terry Olsen <(E-Mail Removed)> wrote:
> Hello Tom. I was trying your method in the following code, but I get this
> error in design time;
> "'AddressOf' expression cannot be converted to 'System.Delegate' because
> 'System.Delegate' is not a delegate type"
> Any Ideas??? Also, would this even work? It seems as if it might be trying
> to read data into myBuffer twice (2nd time when the invoke occurrs).
> Thanks!
>
> Private Sub myListener_DataArrival() Handles myListener.DataArrival
> Debug.Print("DataArrival Event")
> Dim myBuffer() As Byte = New Byte() {}
> myListener.ReadData(myBuffer)
> If txtReceive.InvokeRequired Then
> txtReceive.Invoke(AddressOf myListener_DataArrival) 'Error
> occurrs here.
> Else
> txtReceive.AppendText(System.Text.Encoding.ASCII.GetString(myBuffer))
> End If
> End Sub
>


First of all... Are you using one of the standard .NET classes? Because
none of those have a DataArrival event that I recall. I use this
technique all the time for async and multithreaded calls, so I know it
works..

Anyway, part of the problem would be your structure as well... You
would want to move the code that does the reading inside the else block:

If txtRecieve.InvokeRequired Then
' invoke the method
Else
' dim your buffer
' read the data
' append the text
End If
--
Tom Shelton [MVP]
 
Reply With Quote
 
Terry Olsen
Guest
Posts: n/a
 
      16th Jan 2005
Tom,

I'm trying to use this code...but I'm having some problems.

I'm trying to call a Button.PerformClick method from an event handler that
is called from another thread.

Private Sub ConnectionDroppedEvent() Handles myListener.ConnectionDropped

> That's one way, or you can just invoke your method again...
>
> Private Sub p_recv (ByVal sender As Object, ByVal e As
> SerialRecievedEventArgs) Handles p.ReceivedEvent
>
> If txtRecv.InvokeRequired Then
> txtRecv.Invoke (AddressOf p_recv, new Object() {sender, e})
> Else
> txtRecv.Text += p.ReadExisting
> End If
> End Sub
>
>
> --
> Tom Shelton [MVP]



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why no cross-threading problems with DispatcherTimer? RayLopez99 Microsoft C# .NET 2 14th Feb 2010 10:06 AM
Cross-threading with backgroundworker - passing listview cpix Microsoft Dot NET 2 3rd Aug 2007 10:13 AM
newbie--Threading--sample code--Help (Cross-thread operation not v =?Utf-8?B?bWFnZWxsYW4=?= Microsoft VB .NET 3 2nd Jun 2007 03:10 PM
Threading & Events Causing Cross Thread Problems JasonX Microsoft C# .NET 5 24th Apr 2007 11:49 AM
Re: Need help with multi threading, got error "Cross-thread operation. Jon Skeet [C# MVP] Microsoft C# .NET 0 8th Jan 2007 07:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:05 PM.