473,499 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Illegal cross-thread operation

rob
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:

I have a main form "MainForm". This form contains another form "form1".
This form in return contains a panel "panel1" with controls. "form1"
and "panel1" have an event "updated". "MainForm" did sign up a delegate
for that even in "form1". "form1" did the same for "panel1"'s event.
The delegate for "form1" simply calls all the delegates that did sign
up to "form1". So if something in "panel1" happens it calls the
delegate of "form1" which in return calls the delegate of "MainForm".
In "MainForm"'s delegate I call a function in "form1" which in return
calls a function in "panel1". This is when I get the above exception.

Regards,
Rob

Nov 17 '05 #1
2 6659
delegates are not guranteed to be called on the same thread that
created\assigned the delegate.

You will have to invoke the update method on the control to get the control
to update on the UI thread.
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.

"rob" <rm*******@yahoo.com> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
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:

I have a main form "MainForm". This form contains another form "form1".
This form in return contains a panel "panel1" with controls. "form1"
and "panel1" have an event "updated". "MainForm" did sign up a delegate
for that even in "form1". "form1" did the same for "panel1"'s event.
The delegate for "form1" simply calls all the delegates that did sign
up to "form1". So if something in "panel1" happens it calls the
delegate of "form1" which in return calls the delegate of "MainForm".
In "MainForm"'s delegate I call a function in "form1" which in return
calls a function in "panel1". This is when I get the above exception.

Regards,
Rob

Nov 17 '05 #2
A delegate will only not be invoked on the same thread if something somewhere is performing either an async operation (calling BeginXXX, e.g. BeginInvoke on a delegate); someone lauches their owen thread via the thread class and raises the event or you are using a System.Threading.Timer or a System.Timers.Timer that isn't synchronized.

As long as everyone is running on the UI thread, all UI elements can talk to eachother (assuming accessiblity of methods, etc). I assume you are running v2.0 of Windows Forms here as 1.1 did not throw that exception.

Where is the exception being raised? When an update of the panel occurs or the form or the MainForm?

It sounds to me like something is running an async method and has rquested a completion callback (via an AsyncCallback delegate). This will execute on a thread pool thread and so will not be able to update the UI directly. Instead it should call BeginInvoke on the UI element it wants to touch. Code can check wither its running on the UI thread by checking the InvokeRequired member of the UI element it wants to call - the returns false if the code is running on the UI thread, true otherwise.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

delegates are not guranteed to be called on the same thread that
created\assigned the delegate.

You will have to invoke the update method on the control to get the control
to update on the UI thread.
--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.

"rob" <rm*******@yahoo.com> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
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:

I have a main form "MainForm". This form contains another form "form1".
This form in return contains a panel "panel1" with controls. "form1"
and "panel1" have an event "updated". "MainForm" did sign up a delegate
for that even in "form1". "form1" did the same for "panel1"'s event.
The delegate for "form1" simply calls all the delegates that did sign
up to "form1". So if something in "panel1" happens it calls the
delegate of "form1" which in return calls the delegate of "MainForm".
In "MainForm"'s delegate I call a function in "form1" which in return
calls a function in "panel1". This is when I get the above exception.

Regards,
Rob


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
42339
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
5
3801
by: Dave | last post by:
I need to cut out illegal characters in a string submitted from a mobile phone to a web form. I need a way to check for the illegal characters in a textbox. I intend to loop through the text and...
134
8914
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
1
13026
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
10
14087
by: webmaster | last post by:
Getting error: Illegal characters in path what's wrong with this? string xmlSR = "<XmlDS><table1><phone>Value1</phone><name>jake</name></table1></XmlDS>"; myDataSet.ReadXml(xmlSR,...
7
1924
by: salvador | last post by:
Hi there, I've been working on a web site, and am running into a cross-browser css problem that I'm not quite sure how to solve. I have some twindows that use absolute positioning and the...
5
10069
by: Michael Reichenbach | last post by:
After working with script languages, notepad(++) and co. and several other ide`s I found something which really improved my productivity (Visual Studio 2005). It`s imho better then dev-cpp... I...
34
1726
by: raphfrk | last post by:
This program should copy one file onto the other. It works if I compile it with gcc to a cygwin program. However, if I compile it with the -mno-cygwin option, it doesn't work (this targets native...
2
17797
by: Kamaria | last post by:
I wrote this program to calculate the income tax of a user depending on whether or not they were married: import javax.swing.JOptionPane; public class Income_Tax { public static void main...
15
3584
by: Fuzz13 | last post by:
I'm writing a program that a user loads a file (txt or csv) in which it reads newlines and commas as new object delimiters. Each new object is put into an array creating a new array object each time....
0
7007
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...
1
6894
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...
0
7388
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...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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...
0
3099
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...
0
1427
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 ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
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...

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.