handling events fired by other threads
-
Hi, I have a class running on a secondary thread that listens to an IRC channel (don't worry if you aren't familiar with IRC, all this code is working), and fires an event when it receives a message, passing on this message on. The problem is that I need the main thread to do all the business once a message is received. Attaching events in the normal manner causes the code fired by the event to be run on the secondary thread, which is not good. So basically; how can I attach an event handler that runs on my main thread to events fired on a secondary thread? Hope that all makes sense, I have done lots of searching but cannot find what I'm looking for, this is partly because I'm not really sure what I am looking for, as I am totally stuck. thanks Martin
-
Hi, I have a class running on a secondary thread that listens to an IRC channel (don't worry if you aren't familiar with IRC, all this code is working), and fires an event when it receives a message, passing on this message on. The problem is that I need the main thread to do all the business once a message is received. Attaching events in the normal manner causes the code fired by the event to be run on the secondary thread, which is not good. So basically; how can I attach an event handler that runs on my main thread to events fired on a secondary thread? Hope that all makes sense, I have done lots of searching but cannot find what I'm looking for, this is partly because I'm not really sure what I am looking for, as I am totally stuck. thanks Martin
AFAIK there's no other way than doing the following inside each event handler.
private void _Click(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(this._Click), new object[] { sender, e });
return;
}// Handle the event
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
AFAIK there's no other way than doing the following inside each event handler.
private void _Click(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(this._Click), new object[] { sender, e });
return;
}// Handle the event
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook