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

Avoiding Cross thread calls

P: n/a
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?
Jun 6 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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" <Da*****@vestryonline.com> wrote in message
news:uV**************@TK2MSFTNGP05.phx.gbl...
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?

Jun 6 '06 #2

P: n/a
> 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?


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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jun 6 '06 #3

P: n/a
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" <ED***@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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?


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: ED***@mvps.org
WWW: Http://EDais.mvps.org/

Jun 6 '06 #4

P: n/a
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:
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?


Jun 6 '06 #5

P: n/a
On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <Da*****@vestryonline.com>
wrote:
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?

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
Jun 6 '06 #6

P: n/a
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" <Da*****@vestryonline.com> wrote in message
news:uV**************@TK2MSFTNGP05.phx.gbl...
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?

Jun 6 '06 #7

P: n/a
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" <no****@here.com> wrote in message
news:n1********************************@4ax.com...
On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <Da*****@vestryonline.com>
wrote:
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?

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

Jun 6 '06 #8

P: n/a
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" <sk**@skle.com> wrote:
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" <no****@here.com> wrote in message
news:n1********************************@4ax.com.. .
On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <Da*****@vestryonline.com>
wrote:
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?

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

Jun 6 '06 #9

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

Jun 9 '06 #10

P: n/a
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

th**********@gmail.com wrote:
i'm going to write a telnet library please help me to initiate it .with
out any third party tool


Jun 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.