开发者

C# Event Handlers Using an Enum

开发者 https://www.devze.com 2022-12-25 12:06 出处:网络
I have a StatusChanged event that is raised by my object when its status changes - however, the application needs to carry out additional actions based on what the new status is.

I have a StatusChanged event that is raised by my object when its status changes - however, the application needs to carry out additional actions based on what the new status is.

e.g If the new status is Disconnected, then it must update the status bar text and send an email notification.

So, I wanted to create an Enum with the possible statuses (Connected, Disconnected, ReceivingData, SendingData etc.) and have that sent with the EventArgs parameter of the event when it is raised (see below)

Define the object:

class ModemComm
{
    public event CommanderEventHandler ModemCommEvent;
    public delegate void CommanderEventHandler(object source, ModemCommEventArgs e);

    public void Connect()
    {
        ModemCommEvent(this, new ModemCommEventArgs ModemCommEventArgs.eModemCommEvent.Connected));
    }
}

Define the new EventArgs parameter:

public class ModemCommEventArgs : EventArgs{
    public enum eModemCommEvent
    {
        Idle,
        Connected,
        Disconnected,
        SendingData,
        ReceivingData
    }

    public eModemCommEvent eventType { get; set; }
    public string eventMessage { get; set; }

    public ModemCommEventArgs(eModemCommEvent eventType, string eventMessage)
    {
        thi开发者_开发知识库s.eventMessage = eventMessage;
        this.eventType = eventType;
    }
}

I then create a handler for the event in the application:

ModemComm comm = new ModemComm();
comm.ModemCommEvent += OnModemCommEvent;

and

private void OnModemCommEvent(object source, ModemCommEventArgs e)
{
}

The problem is, I get a 'Object reference not set to an instance of an object' error when the object attempts to raise the event. Hoping someone can explain in n00b terms why and how to fix it :)


Events are null when no clients are subscribed to them, so an attempt to invoke an event that has no subscribers will fail with a NullReferenceException.

Some common techniques to avoid this:

1) Check for null in a thread-safe manner (from the event raiser's perspective; there is still a race condition on the client, however it's their responsibility to be handle that)

var handler = this.ModemCommEvent;
if( handler != null ) {
    handler(this, new ModemCommEventArgs( ModemCommEventArgs.eModemCommEvent.Connected ));
}

The above code is the more complex version of this:

if( this.ModemCommEvent != null ) {
    this.ModemCommEvent(this, new ModemCommEventArgs(ModemCommEventArgs.eModemCommEvent.Connected));
}

The first one (that creates a local variable) is safer from the event raiser's perspective because the local variable will either be null or it won't and nothing will change that. In the second one, however, a client running on a separate thread could unsubscribe from the event between the time your check for null is done and you raise the event. In that case, you'd end up with a NullReferenceException again. If neither you nor your code's clients are executing on multiple threads (no BackgroundWorker, Thread object, asynchronous invoke, etc), then the safer check is redundant. If you're not sure, however, it's a good practice to get into. That, or do #2.

2) Default your event to an empty value

public event CommanderEventHandler ModemCommEvent = delegate { };

This side-steps the issue completely by always having at least one subscriber. The "delegate {}" syntax creates an anonymous method that does nothing that is the "default subscriber" to the event. No matter how many clients subscribe or unsubscribe from your event, this anonymous method will always be there, preventing your event from being null.

--

This has been discussed ad nauseum all over the internet. Here is one such example:

C# Events and Thread Safety


try

public void Connect()
{
    if( ModemCommEvent != null)
    ModemCommEvent(this, new ModemCommEventArgs ModemCommEventArgs.eModemCommEvent.Connected));
}

probably, your Connect is launched before the handler is added?

0

精彩评论

暂无评论...
验证码 换一张
取 消