In the following code MessageReceived
is on a different thread to label1
and when trying to access it I will get this error:
Make always sure you update your GUI on the main thread (GUI thread).
foo.MessageReceived += new Agent.MessageReceivedHandler(foo_MessageReceived);
public delegate void MyDelegate(Message msg);
void foo_MessageReceived(Message message)
{
if (InvokeRequired)
{
BeginInvoke(new MyDelegate(foo_MessageReceived),new object[]{message});
}
else
{
label1.Text = message.Body;
}
}