I've inherited a C# (.NET 2.0) app that has a bunch of static methods. I need to turn one of these methods into an asynchronous event-based method. When the method is complete, I want to fire off an event-handler. My question is, can I fire off an event handler from a static method? If so, how?
When I google, I only find IAsyncResult examples. However, I want to be able to do something like the following:
EventHandler myEvent_Completed;
public void DoStuffAsync()
{
// Asynchrously do stuff that may take a while
if (myEvent_Completed != null)
myEvent_Completed(this, EventArgs.Empty);
}
Thank you!
The process is exactly the same, the only difference is there isn't really a this
reference.
static EventHandler myEvent_Completed;
public void DoStuffAsync()
{
FireEvent();
}
private static void FireEvent()
{
EventHandler handler = myEvent_Completed;
if (handler != null)
handler(null, EventArgs.Empty);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With