Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Static Methods with Event Handlers

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!

like image 673
Bill Jones Avatar asked Sep 16 '25 00:09

Bill Jones


1 Answers

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);
}
like image 167
Lloyd Avatar answered Sep 17 '25 14:09

Lloyd