Multithreading problem using C# for a network app

G

Guest

Hi everyone,
I used multithreading for a network application using Winsock. so when I
receive a message in my secondary thread, I raise an event to upper layer and
in that event handler when I try to fill a treeview control with the
appropriate information I get the following error:
"The action being performed on this control is being called from the wrong
thread. You must marshal to the correct thread using Control.Invoke or
Control.BeginInvoke to perform this action"
could anyone help me plzzzzzz?

Many tnx in advance
 
C

Carl Daniel [VC++ MVP]

Jefe said:
Hi everyone,
I used multithreading for a network application using Winsock. so when I
receive a message in my secondary thread, I raise an event to upper layer
and
in that event handler when I try to fill a treeview control with the
appropriate information I get the following error:
"The action being performed on this control is being called from the wrong
thread. You must marshal to the correct thread using Control.Invoke or
Control.BeginInvoke to perform this action"
could anyone help me plzzzzzz?

Do what the error message tells you - use Control.Invoke or
Control.BeginInvoke to cause the UI to be updated in the correct thread.
Look the functions up on MSDN.

-cd
 
T

Tom Spink

Jefe said:
Hi everyone,
I used multithreading for a network application using Winsock. so when I
receive a message in my secondary thread, I raise an event to upper layer
and in that event handler when I try to fill a treeview control with the
appropriate information I get the following error:
"The action being performed on this control is being called from the wrong
thread. You must marshal to the correct thread using Control.Invoke or
Control.BeginInvoke to perform this action"
could anyone help me plzzzzzz?

Many tnx in advance

Hi Jefe,

You need to execute code that updates your UI in the context of your UI
thread. To do this, you need to use the Invoke method of your form to
execute a method in the correct context.

///
private void CallSafeMethod ( )
{
if ( this.InvokeRequired )
this.Invoke( new UiSafeMethodDelegate( UiSafeMethod ) );
else
UiSafeMethod();
}

private delegate void UiSafeMethodDelegate ( );
private void UiSafeMethod ( )
{
...
}
///

Any code in UiSafeMethod will be called in the context of your UI thread, if
you call 'CallSafeMethod'.
 
G

Guest

Hi Tom,
tnx for your reply, it helped me solve my problem. but I want to know why
such an error should occur? I mean why I can not fill treeview from an event
handler that is raised from a secondary thread?
tnx
 
C

Carl Daniel [VC++ MVP]

Jefe said:
Hi Tom,
tnx for your reply, it helped me solve my problem. but I want to know
why such an error should occur? I mean why I can not fill treeview
from an event handler that is raised from a secondary thread?
tnx

Because that's the way Windows works. All operations on a single window
must occur in the context of the thead that created that window. Winforms
controls are just wrappers around Win32 windows, so they have the same
restriction.

They could have made the Winforms controls automatically marshall the
request into the right thread, but there are issues with doing so. First,
there's a performance cost. The designers decided (I think rightly so) that
the programmer (i.e. you) should make a conscious decision to incur that
cost by explicitly marshalling the call into the correct thread yourself.
Second, there are potential deadlock issues with Control.Invoke (or Win32's
SendMessage). If they handled it automatically for you, and you found your
way into one of those deadlock cases, you'd be stuck. By giving you
control, you can avoid those deadlock cases (primarily by calling
Control.BeginInvoke instead of Control.Invoke).

-cd
 

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