473,563 Members | 2,797 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 6667
delegates are not guranteed to be called on the same thread that
created\assigne d 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*******@yaho o.com> wrote in message
news:11******** ************@l4 1g2000cwc.googl egroups.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.Threadin g.Timer or a System.Timers.T imer 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\assigne d 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*******@yaho o.com> wrote in message
news:11******** ************@l4 1g2000cwc.googl egroups.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.publi c.dotnet.langua ges.csharp]
Nov 17 '05 #3

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

Similar topics

14
42373
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 = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
5
3809
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 remove the illegal characters. Is there an equivalent function to c# to chr() in Visual basic? I want the decimal value of a character, like "A" is...
134
8979
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
1
13034
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
10
14102
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, XmlReadMode.IgnoreSchema); DataList5.DataSource = myDataSet; DataList5.DataBind(); Also, what's the best way to return XML from a method for binding into
7
1931
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 visibility property to display some instructions to a screen. They render properly on my page in Mozilla, but in IE they render to the side of any...
5
10077
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 worked with C# and VB.net a bit and it was really easy because of the debugger, the interactive debugger, intellisense, command completer and so on. ...
34
1747
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 windows). Anyway, I just want to check that the program is valid before I see if I can find a way around a compiler bug. It might be...
2
17804
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 (String args) {
15
3592
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. There is also several tests in place to check the data being read into the program to return an error if there are characters that can't be used for...
0
7665
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...
0
7583
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...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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...
1
2082
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
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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...

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.