Illegal cross-thread operation

R

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
 
O

Ollie Riches

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.
 
R

Richard Blewett [DevelopMentor]

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 said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top