473,506 Members | 13,088 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

Jul 21 '05 #1
2 1876
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

Jul 21 '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]
Jul 21 '05 #3

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

Similar topics

2
1760
by: Frances Del Rio | last post by:
Pls, what are all illegal chars in e-mail addreses? specif. I'd like to know if apostrophe is allowed, but would be handy to find a list of all illegal chars for e-mail addresses.. searched...
14
42344
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
3802
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...
0
314
by: Felix Finch | last post by:
I have a perl test program which has about 80 test cases, each of which creates its own schema so I can remove them with DROP SCHEMA xxx CASCADE. Normally each test case creates and drops the same...
134
8918
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
1
13027
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
10
14088
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,...
8
4778
by: Just another C hacker | last post by:
Hello friends, I'm writing a program in C with some bits in inline asm for efficiency. I'd like to be able to handle illegal instructions from within asm. Here's an example of a standalone asm...
34
1728
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...
0
7220
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,...
0
7105
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
5617
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
5037
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
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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.