473,408 Members | 2,477 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,408 software developers and data experts.

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 1873
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
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
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
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
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
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
10
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
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
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
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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
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.