In the following code MessageReceived
is on a different thread to label1
and when trying to access it I will get this error:
Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
foo.MessageReceived += new Agent.MessageReceivedHandler(foo_MessageReceived);
void foo_MessageReceived(Message message)
{
label1.Text = message.Body;
}
How can I solve this?
More: apparently I need to use delegate
and invoke
, but I don't know how, can you please explain in more detail?