473,799 Members | 3,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6892
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 UpdateGuiCallBa ck(_gui.Update) );

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

"Daniel" <Da*****@vestry online.com> wrote in message
news:uV******** ******@TK2MSFTN GP05.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 CheckForIllegal CrossThreadCall s 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:

CheckForIllegal CrossThreadCall s

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******** ********@TK2MSF TNGP05.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 CheckForIllegal CrossThreadCall s 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:

CheckForIllegal CrossThreadCall s

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*****@vestry online.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 BackgroundWorke r object in System.Componen tModel. There is a
pretty good example here

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

BackgroundWorke r 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 BackgroundWorke r, 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 UpdatelistBoxDe legate(Object data);

public void UpdateListBox(O bject data)
{
if(this.InvokeR equired)
{
this.Invoke(new UpdatelistBoxDe legate(UpdateLi stBox(data));
return;
}

// do code to update ListBox
}

"Daniel" <Da*****@vestry online.com> wrote in message
news:uV******** ******@TK2MSFTN GP05.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.co m> wrote in message
news:n1******** *************** *********@4ax.c om...
On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <Da*****@vestry online.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 BackgroundWorke r object in System.Componen tModel. There is a
pretty good example here

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

BackgroundWorke r 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 BackgroundWorke r, 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 BackgroundWorke r 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.co m> 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.co m> wrote in message
news:n1******* *************** **********@4ax. com...
On Tue, 6 Jun 2006 10:49:47 +0100, "Daniel" <Da*****@vestry online.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 BackgroundWorke r object in System.Componen tModel. There is a
pretty good example here

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

BackgroundWorke r 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 BackgroundWorke r, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1901
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 function from another form even if there is a parent-child relationship? Aren't controls like TreeView based on forms. If so then why can a form containing a TreeView call members of the TreeView? If you're interested here is my scenario:
8
4856
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 I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
3
2349
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 object system that is exposed to .NET through COM, and I'm really trying to avoid slow cross-apartment calls. Also, is using Invoke() on a class created from the main thread a solution to making COM calls into that thread? Thanks.
4
5321
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 CardReader class which validates the number etc. Neither of these classes inherit from any other classes / controls. The SerialComm class is instantiated by the Cardreader class, so in the
15
2609
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, the main thread waits. At the completion of each subthread, the mainthread checks all 12 thread objects to see if they are done. If they are, raise an event that says we're done. So, it's kinda like this: ProcessThread - Creates a ProcessObject
15
2376
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 = new Thread(new ThreadStart(RunServer)); readThread.Start(); attemt to update the textbox (in the function RunServer). I get an exception saying that cross thread operation is invalid as there is an attempt to access a texbox created in a nother...
5
3028
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 realize it's not wise, and in some circumstances, not even possible to update the controls from the stream other than the main stream. I believe there are two recommended ways to handle this situation. 1. Use Invoke() to "call" an updater method...
5
3606
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 hope perhaps some experts here can help me out. Here is what my program consists of: 1) A form containng several tabs, one of which contains a ListView
6
2377
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 socket that there is message to be processed. But this is not working as the socket receiver method seems to be in a different thread then the form that owns the socket. The receive callback gets the data from the socket and process the...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.