Threads and GUI updating

  • Thread starter Thread starter Leif Eirik Olsen
  • Start date Start date
L

Leif Eirik Olsen

Hi,

I have a main form frmMain, frmMain creates an instance of a class MyThread.
MyThread creates a thread and countiously reads a comport for some string
data.
When a certain string is recieved MyThread fires an event OnDataRecieved.
frmMain has an eventhandeler implemented (DataRecieved) for the
MyThread.OnDataRecieved event.

So finally my question :)
Is it safe to update textbox and label values from within
frmMain.DataRecieved ?

Any help or hints greatly appriciated.

regards,
Leo
 
So finally my question :)
Is it safe to update textbox and label values from within
frmMain.DataRecieved ?

Absolutely not. It's not safe in the CF or full framework. Use
Control.Invoke to marshal a call back to the primary UI thread and update
from there.

-Chris
 
Absolutely not. It's not safe in the CF or full framework. Use
Control.Invoke to marshal a call back to the primary UI thread and update
from there.

Ok, I see :)

I understand I need to do something like this from MyThread :

frmMain.Invoke(new Eventhandler(aGuiUpdateMethodeInfrmMain);
right?

If this is correct is there a more 'dynamic' way to do the call. I do not
like the idea tha MyThread needs to know both frmMain and
aGuiUpdateMethodeInfrmMain.

Could mabe MyThread take a inparameter ('methode to invkoke'?) when
constructed ? Tried this, but with no luck so far.

regards,
Leo
 
Back
Top