简体   繁体   中英

C# - Question about Events

I am very new to using C# event handling. Our application has the following events defined:

public sealed class MonitorCallback : IMonitorCallback
{
    public event EventHandler<ApplyEventArgs> ApplyAccepted;
    public event EventHandler<ApplyEventArgs> ApplyRejected;
}

I need to write some code to handle and respond these events when they are fired. Could someone get me started as to how I might do this?

Visual Studio will automatically create stubs for the event handler function when you start typing the += below and hit TAB.

protected MyMonitorCallback MonitorCallback;
public class MyClass
{
     void Main()
     {
          MyMonitorCallback = new MonitorCallback();
          MyMonitorCallback.ApplyAccepted += new EventHander<ApplyEventArgs>(MyMonitorCallback_ApplyAccepted);
     }
     void MyMonitorCallback_ApplyAccepted(object sender, ApplyEventArgs e) {
        ..
     }
}

The best place to start would be the msdn tutorial This will go through declaring, invoking and hooking up an event.

It might also be a good idea to have a read about delegates ( another msdn tutorial ) as you would use them in your event handling and it would be a good idea to get an understanding from the ground up.

When you create the MonitorCallback instance, you subscribe the events to the event handler that you are going to write. Syntax would look something like,

 public class MonitorCallbackFactory{

     public MonitorCallback CreateCallback(){
         //  create the callback instance
         var callback = new MonitorCallback();

         // subscribe the events to the EventHandler.  
         callback.ApplyAccepted += OnApplyAccepted;
         callback.ApplyRejected += OnApplyRejected;

         return callback;
     } 

     protected virtual void OnApplyAccepted(object sender, ApplyEventArgs e){
           // the sender is always the type of object that raises the event, so
           // if you need it strongly typed you can do:
           var callback = (MonitorCallback)sender;
           // then write your code for what happens when
           // the ApplyAccepted event is raised here  
     }

     protected virtual void OnApplyRejected(object sender, ApplyEventArgs e){
           // write your code for what happens when
           // the ApplyRejected event is raised here  
     }

 }

As you can see the += is the syntax to subscribe a handler to an event. -= is the syntax to unsubscribe and event handler.

I presume you already have code like this;

public class EventExample
{
    private EventHandler<ApplyEventArgs> m_evApplyAccepted;
    public event EventHandler<ApplyEventArgs> ApplyAccepted
    {
        add { m_evApplyAccepted += value; }
        remove { m_evApplyAccepted -= value; }
    }
    protected virtual void OnEventName(ApplyEventArgs e)
    {
        if (m_evApplyAccepted != null)
            m_evApplyAccepted.Invoke(this, e);
    }
    public class ApplyEventArgs : EventArgs { }
}

You consume this event like this;

public class EventConsumer
{
    private EventExample eventExample;
    public EventConsumer()
    {
        eventExample = new EventExample();

        //register a handler with the event here
        eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
    }

    void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
    {
        //respond to event here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM