Connecting Tech Pros Worldwide Forums | Help | Site Map

Avoiding Cross thread calls

Daniel
Guest
 
Posts: n/a
#1: Jun 6 '06
Hi guys

I have a form with my gui on, just some list boxes.

And a class that handles incoming data.

i want to, on receiving data, update my gui.

However even though i have an instance created in my class handling the
receiving data of the gui form if i try and update the gui directly i get a
cross thread error, trying to access the form listbox from a thread other
that the one it was created on.

its because the method that called the gui update method is asynchronous.

How do i get round this, i have tried using events and delegates getting the
class to, on receving data, fire an event and then hook that event to the
gui so it updates on the event firing but this still causes the same error.

This must be possible right?



Daniel
Guest
 
Posts: n/a
#2: Jun 6 '06

re: Avoiding Cross thread calls


SORRY solved it.

For anyone else with a similar problem you need to use Invoke to call the
update fromt he controls thread.

So in my case in my Class taking the incoming connections i do something
like

_gui.Invoke(new UpdateGuiCallBack(_gui.Update));

http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx



"Daniel" <DanielV@vestryonline.com> wrote in message
news:uVIVK6UiGHA.3900@TK2MSFTNGP05.phx.gbl...[color=blue]
> Hi guys
>
> I have a form with my gui on, just some list boxes.
>
> And a class that handles incoming data.
>
> i want to, on receiving data, update my gui.
>
> However even though i have an instance created in my class handling the
> receiving data of the gui form if i try and update the gui directly i get
> a cross thread error, trying to access the form listbox from a thread
> other that the one it was created on.
>
> its because the method that called the gui update method is asynchronous.
>
> How do i get round this, i have tried using events and delegates getting
> the class to, on receving data, fire an event and then hook that event to
> the gui so it updates on the event firing but this still causes the same
> error.
>
> This must be possible right?
>[/color]


Mike D Sutton
Guest
 
Posts: n/a
#3: Jun 6 '06

re: Avoiding Cross thread calls


> I have a form with my gui on, just some list boxes.[color=blue]
>
> And a class that handles incoming data.
>
> i want to, on receiving data, update my gui.
>
> However even though i have an instance created in my class handling the receiving data of the gui form if i try and
> update the gui directly i get a cross thread error, trying to access the form listbox from a thread other that the one
> it was created on.
>
> its because the method that called the gui update method is asynchronous.
>
> How do i get round this, i have tried using events and delegates getting the class to, on receving data, fire an event
> and then hook that event to the gui so it updates on the event firing but this still causes the same error.
>
> This must be possible right?[/color]

Try setting the CheckForIllegalCrossThreadCalls property of your form to false. Alternatively have a look here:
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/


Daniel
Guest
 
Posts: n/a
#4: Jun 6 '06

re: Avoiding Cross thread calls


Hi Mike

Solved it already but thanks. Though your reply was interesting, if i was to
use the:

CheckForIllegalCrossThreadCalls

property, wouldn't that be dangerous to surpress the check? Couldn't i have
bugs in my software caused by the cross thread?

"Mike D Sutton" <EDais@mvps.org> wrote in message
news:%23tUB1GViGHA.4572@TK2MSFTNGP05.phx.gbl...[color=blue][color=green]
>> I have a form with my gui on, just some list boxes.
>>
>> And a class that handles incoming data.
>>
>> i want to, on receiving data, update my gui.
>>
>> However even though i have an instance created in my class handling the
>> receiving data of the gui form if i try and update the gui directly i get
>> a cross thread error, trying to access the form listbox from a thread
>> other that the one it was created on.
>>
>> its because the method that called the gui update method is asynchronous.
>>
>> How do i get round this, i have tried using events and delegates getting
>> the class to, on receving data, fire an event and then hook that event to
>> the gui so it updates on the event firing but this still causes the same
>> error.
>>
>> This must be possible right?[/color]
>
> Try setting the CheckForIllegalCrossThreadCalls property of your form to
> false. Alternatively have a look here:
> http://msdn2.microsoft.com/en-us/library/ms171728.aspx
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@mvps.org
> WWW: Http://EDais.mvps.org/
>[/color]


Brian Gideon
Guest
 
Posts: n/a
#5: Jun 6 '06

re: Avoiding Cross thread calls


Daniel,

The check is provided by a Managed Debugging Assistant (MDA). It is
only compiled into debug builds. I believe the check will also occur
when the property is set to true in release builds. But, yeah,
accessing a control from a thread other than the UI thread would be
dangerous.

Brian

Daniel wrote:[color=blue]
> Hi Mike
>
> Solved it already but thanks. Though your reply was interesting, if i was to
> use the:
>
> CheckForIllegalCrossThreadCalls
>
> property, wouldn't that be dangerous to surpress the check? Couldn't i have
> bugs in my software caused by the cross thread?
>[/color]

Steve
Guest
 
Posts: n/a
#6: Jun 6 '06

re: Avoiding Cross thread calls


On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <DanielV@vestryonline.com>
wrote:
[color=blue]
>Hi guys
>
>I have a form with my gui on, just some list boxes.
>
>And a class that handles incoming data.
>
>i want to, on receiving data, update my gui.
>
>However even though i have an instance created in my class handling the
>receiving data of the gui form if i try and update the gui directly i get a
>cross thread error, trying to access the form listbox from a thread other
>that the one it was created on.
>
>its because the method that called the gui update method is asynchronous.
>
>How do i get round this, i have tried using events and delegates getting the
>class to, on receving data, fire an event and then hook that event to the
>gui so it updates on the event firing but this still causes the same error.
>
>This must be possible right?
>[/color]


Use the BackgroundWorker object in System.ComponentModel. There is a
pretty good example here

http://msdn2.microsoft.com/en-us/sys...undworker.aspx

BackgroundWorker is very easy to use.

The link above contains detailed info and the example (c#/vb/cpp) is
clear, I used it as a base for my own asynchronous functions when I
ran into the same problem as yourself, so I know it works. If you need
a hand let me know. Just to say, I tried several methods before
deciding on using BackgroundWorker, it is certainly suitable for the
task.

chers

Steve

http://www.pretty-vacant.co.uk
Steve
Guest
 
Posts: n/a
#7: Jun 6 '06

re: Avoiding Cross thread calls


I add something like this to UI methods that could be called from other
threads

[pseudo code]
private delegate void UpdatelistBoxDelegate(Object data);

public void UpdateListBox(Object data)
{
if(this.InvokeRequired)
{
this.Invoke(new UpdatelistBoxDelegate(UpdateListBox(data));
return;
}

// do code to update ListBox
}



"Daniel" <DanielV@vestryonline.com> wrote in message
news:uVIVK6UiGHA.3900@TK2MSFTNGP05.phx.gbl...[color=blue]
> Hi guys
>
> I have a form with my gui on, just some list boxes.
>
> And a class that handles incoming data.
>
> i want to, on receiving data, update my gui.
>
> However even though i have an instance created in my class handling the
> receiving data of the gui form if i try and update the gui directly i get
> a cross thread error, trying to access the form listbox from a thread
> other that the one it was created on.
>
> its because the method that called the gui update method is asynchronous.
>
> How do i get round this, i have tried using events and delegates getting
> the class to, on receving data, fire an event and then hook that event to
> the gui so it updates on the event firing but this still causes the same
> error.
>
> This must be possible right?
>[/color]


Steve
Guest
 
Posts: n/a
#8: Jun 6 '06

re: Avoiding Cross thread calls


I forgot to mention why I like this. Rather than put the Invoke logic in
each location that might make a call to the UI methods, I only need to add
it to one place, the UI method. So I could have 20 different classes,
services, events etc all hitting that UI method and it will be protected.


"Steve" <nospam@here.com> wrote in message
news:n1cb821i694mdh8p5jndskrnt0110lsjrr@4ax.com...[color=blue]
> On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <DanielV@vestryonline.com>
> wrote:
>[color=green]
>>Hi guys
>>
>>I have a form with my gui on, just some list boxes.
>>
>>And a class that handles incoming data.
>>
>>i want to, on receiving data, update my gui.
>>
>>However even though i have an instance created in my class handling the
>>receiving data of the gui form if i try and update the gui directly i get
>>a
>>cross thread error, trying to access the form listbox from a thread other
>>that the one it was created on.
>>
>>its because the method that called the gui update method is asynchronous.
>>
>>How do i get round this, i have tried using events and delegates getting
>>the
>>class to, on receving data, fire an event and then hook that event to the
>>gui so it updates on the event firing but this still causes the same
>>error.
>>
>>This must be possible right?
>>[/color]
>
>
> Use the BackgroundWorker object in System.ComponentModel. There is a
> pretty good example here
>
> http://msdn2.microsoft.com/en-us/sys...undworker.aspx
>
> BackgroundWorker is very easy to use.
>
> The link above contains detailed info and the example (c#/vb/cpp) is
> clear, I used it as a base for my own asynchronous functions when I
> ran into the same problem as yourself, so I know it works. If you need
> a hand let me know. Just to say, I tried several methods before
> deciding on using BackgroundWorker, it is certainly suitable for the
> task.
>
> chers
>
> Steve
>
> http://www.pretty-vacant.co.uk[/color]


Steve
Guest
 
Posts: n/a
#9: Jun 6 '06

re: Avoiding Cross thread calls


Hi Steve

I ran into a few problems with the InvokeRequired, which ended up
producing similar cross thread errors I think, I was doing something
wrong anyways :-). I went with BackgroundWorker in the end and quickly
had things working as required.


Steve ;)

http://www.pretty-vacant.co.uk




On Tue, 6 Jun 2006 11:05:04 -0700, "Steve" <skle@skle.com> wrote:
[color=blue]
>I forgot to mention why I like this. Rather than put the Invoke logic in
>each location that might make a call to the UI methods, I only need to add
>it to one place, the UI method. So I could have 20 different classes,
>services, events etc all hitting that UI method and it will be protected.
>
>
>"Steve" <nospam@here.com> wrote in message
>news:n1cb821i694mdh8p5jndskrnt0110lsjrr@4ax.com.. .[color=green]
>> On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <DanielV@vestryonline.com>
>> wrote:
>>[color=darkred]
>>>Hi guys
>>>
>>>I have a form with my gui on, just some list boxes.
>>>
>>>And a class that handles incoming data.
>>>
>>>i want to, on receiving data, update my gui.
>>>
>>>However even though i have an instance created in my class handling the
>>>receiving data of the gui form if i try and update the gui directly i get
>>>a
>>>cross thread error, trying to access the form listbox from a thread other
>>>that the one it was created on.
>>>
>>>its because the method that called the gui update method is asynchronous.
>>>
>>>How do i get round this, i have tried using events and delegates getting
>>>the
>>>class to, on receving data, fire an event and then hook that event to the
>>>gui so it updates on the event firing but this still causes the same
>>>error.
>>>
>>>This must be possible right?
>>>[/color]
>>
>>
>> Use the BackgroundWorker object in System.ComponentModel. There is a
>> pretty good example here
>>
>> http://msdn2.microsoft.com/en-us/sys...undworker.aspx
>>
>> BackgroundWorker is very easy to use.
>>
>> The link above contains detailed info and the example (c#/vb/cpp) is
>> clear, I used it as a base for my own asynchronous functions when I
>> ran into the same problem as yourself, so I know it works. If you need
>> a hand let me know. Just to say, I tried several methods before
>> deciding on using BackgroundWorker, it is certainly suitable for the
>> task.
>>
>> chers
>>
>> Steve
>>
>> http://www.pretty-vacant.co.uk[/color]
>[/color]
thilandeneth@gmail.com
Guest
 
Posts: n/a
#10: Jun 9 '06

re: Avoiding Cross thread calls


i'm going to write a telnet library please help me to initiate it .with
out any third party tool

Brian Gideon
Guest
 
Posts: n/a
#11: Jun 9 '06

re: Avoiding Cross thread calls


This question has nothing in common with cross thread calls.

That aside, have you tried using the classes in the System.Net.Sockets
namespace?

Brian

thilandeneth@gmail.com wrote:[color=blue]
> i'm going to write a telnet library please help me to initiate it .with
> out any third party tool[/color]

Closed Thread