From: Anders Eriksson on
Hello,

I use an COM server that creates a number of events. I have created a class
that controls the COM server and that receives the events, it's called
LaserCtrl. This works!

I have a form that uses LaserCtrl and I want to update a textbox in the form
from LaserCtrl. The constructor of LaserCtrl looks like this

private TextBox msg;

public LaserCtrl(TextBox m)
{
msg = m;
}

From a normal method in LaserCtrl this works perfectly
msg.Text = "my nice message";

BUT from the EventHandler method nothing happens! I'm guessing that this is
because the eventhandler method is in another thread?

How do I solve this?

// Anders
--
English is not my first language, so any
insults or strangeness is due to the translation!

--
--
Anders Eriksson
DC Lasersystem
Software Engineer
073 029 45 74
anders(a)dclasersystem.com

From: Alberto Poblacion on
"Anders Eriksson" <andis.eriksson(a)gmail.com> wrote in message
news:31C7C55D-E3E9-430F-983B-A65509CD949E(a)microsoft.com...
> From a normal method in LaserCtrl this works perfectly
> msg.Text = "my nice message";
>
> BUT from the EventHandler method nothing happens! I'm guessing that this
> is because the eventhandler method is in another thread?

Yes, if the event handler is running in another thread, you have to
marshall execution into the thread that created the control before you can
change it. One way to do this is by means of its Invoke method:

msg.Invoke(new MethodInvoker(WriteMyText));
....
private void WriteMyText()
{
msg.Text = "my nice message";
}



From: Anders Eriksson on
Hello Alberto,

> Yes, if the event handler is running in another thread, you have to
> marshall execution into the thread that created the control before you can
> change it. One way to do this is by means of its Invoke method:
>
> msg.Invoke(new MethodInvoker(WriteMyText));
> ...

Two questions.
1. The msg.Invoke should I call it from my eventhandler?
2. How would I do this if I need to have two parameters? E.g
WriteMyText(String msg, Boolean clr)

// Anders

From: Alberto Poblacion on
"Anders Eriksson" <andis.eriksson(a)gmail.com> wrote in message
news:eR$IZnDaKHA.2160(a)TK2MSFTNGP02.phx.gbl...
> Two questions.
> 1. The msg.Invoke should I call it from my eventhandler?
> 2. How would I do this if I need to have two parameters? E.g
> WriteMyText(String msg, Boolean clr)

1. Yes, you call theControl.Invoke from the code that is running in the
other thread, in this case your eventhandler.

2. Invoke takes actually a System.Delegate parameter, so you can use any
delegate instead of MethodInvoker. I am showing below some code that works
with any version of C# including 1.0, but if you have a newer version you
can simplify the code by means of anonymous delegates or lambdas.

delegate void MyInvoker(String msg, Boolean clr);
....
msg.Invoke(new MyInvoker(MyRoutine), new object[]{valueformsg,
valueforclr});
....
private void MyRoutine(String msg, Boolean clr)
{
...
}


From: Anders Eriksson on
Hello Alberto,

Thank you very much!
Everything works!

// Anders
--