Avoiding Cross thread calls

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

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?
 
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: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
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?
 
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
 
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/system.componentmodel.backgroundworker.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
 
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
}
 
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.
 
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
 
i'm going to write a telnet library please help me to initiate it .with
out any third party tool
 
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
 
Back
Top