473,406 Members | 2,847 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.

Avoiding Cross thread calls

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
10 6837
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
> 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
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
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
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
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
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
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
i'm going to write a telnet library please help me to initiate it .with
out any third party tool

Jun 9 '06 #10
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rob | last post by:
Dear All, I am getting an error "Illegal cross-thread operation". I am not creating any threads. I know internally there are many threads running, though. Does that mean that no form can call a...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
3
by: jlamanna | last post by:
I was wondering if there was a utility that could tell you when your C# application is making cross-apartment COM calls. I have a fairly large application that makes extensive use of a 3rd party...
4
by: Paul Cheetham | last post by:
Hi, I have a couple of classes that I am using to read a swipe-card reader attached to the serial port (c# VS 2005 Pro). I have a SerialComm class which actaully reads the serial port, and a...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
15
by: dani kotlar | last post by:
I am trying to make a client-server application to run, but I am running into this problem: in the server Form the call InitializeComponent(); creates a text-box. Later the calls: readThread =...
5
by: temp2 | last post by:
Hello, I have an app that reads data params from a stream and updates controls accordingly. The stream reader is on a different thread than the main thread that created the controls. I fully...
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
6
by: AliR \(VC++ MVP\) | last post by:
Hi everyone, I have a Socket derived class that parses the received data into a message, populates a object with that information and then tries to send an event to inform the owner of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.