Exception in using control in Mainform

G

Gomez

When I call a method by remoting I get the following exception-
"Control 'conrolname' accessed from a thread other than the thread it was
created on"
Can someone advise how to cope with that provided that I must use this call
somehow?
Regards
Gomez
 
A

Alberto Poblacion

Gomez said:
When I call a method by remoting I get the following exception-
"Control 'conrolname' accessed from a thread other than the thread it was
created on"
Can someone advise how to cope with that provided that I must use this
call somehow?

You can transfer control from the remoting thread into the thread that
created the control by means of the method "Invoke" that all controls have.

For instance, if you are doing theControl.theProperty=theValue; in your
remoted method, you can replace it with this:

theControl.Invoke(new MethodInvoker(myMethod));
...
private void myMethod()
{
theControl.theProperty=theValue;
}

Of course, there are many ways to write the preceding, such as using an
anonymous method inside the call to Invoke instead of the separate myMethod.
This would let you 'capture' theValue instead of passing it around in a
member value as would be required by the previous code example.
 
I

Ignacio Machin ( .NET/ C# MVP )

When I call a method by remoting I get the following exception-
"Control 'conrolname' accessed from a thread other than the thread it was
created on"
Can someone advise how to cope with that provided that I must use this call
somehow?
Regards
Gomez

You have to use Control.Invoke to make sure that y ou modify the
control from the UI thread. If you do a search for "Control.Invoke" in
this NG you will find a lot of posts about this
 

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