Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about custom events

Tags:

c#

.net

events

I'm making custom events for C# and sometimes it isn't working.

This is how I'm making the event happen:

    private bool isDoorOpen;
    public bool IsDoorOpen {
        get { return isDoorOpen;}
        private set { isDoorOpen = value; DoorsChangeState(this, null);}
    }

And these are the event declarations:

    //events        
    public delegate void ChangedEventHandler(Elevator sender, EventArgs e);
    public event ChangedEventHandler PositionChanged;
    public event ChangedEventHandler DirectionChanged;
    public event ChangedEventHandler BreaksChangeState;
    public event ChangedEventHandler DoorsChangeState;

This works as long as there are methods attached to the events, but if there isn't, it throws a null ref exception. What am I doing wrong?

like image 430
Malfist Avatar asked May 07 '10 06:05

Malfist


People also ask

What is the purpose of custom events?

Instead of focusing on the element that triggers an action, custom events put the spotlight on the element being acted upon. This brings a bevy of benefits, including: Behaviors of the target element can easily be triggered by different elements using the same code.

What are custom questions?

Types of custom questions Single choice: a question followed by a list of answers, of which respondents are only allowed to choose one answer option (see example below). Multi choice: a question where respondents are allowed to select as many answering options as they see fit from the ones provided.

How do you define a custom event?

Custom event definitions are event definitions that have been created from scratch in the event definition editor rather than having been generated from existing events by the event definition generator.

Can we create custom events?

Creating a Custom Event: To create a custom event we use the Event constructor or CustomEvent interface. The Event constructor creates an Event and CustomEvent creates an Event with more functionality. The below steps are followed in order to create one using a new Event. We create an event using the Event constructor.


2 Answers

The recommended way to call an event is

var handler = this.DoorsChangeState;
if (handler != null)
    handler(this, null);

The reason for copying the handler locally is incase the event handler changes on another thread while you're checking for null.

EDIT: Found the article talking about race conditions. http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx

like image 107
Cameron MacFarland Avatar answered Nov 03 '22 12:11

Cameron MacFarland


I know this question has been discussed (and answered) several times here on SO.

Also somewhere here i got the following extension methods to make this pattern more easy to use:

public static class EventHandlerExtensions
{
    public static void FireEvent<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs
    {
        var temp = handler;
        if (temp != null)
        {
            temp(sender, args);
        }
    }

    public static void FireEvent(this EventHandler handler, object sender)
    {
        var temp = handler;
        if (temp != null)
        {
            temp(sender, EventArgs.Empty);
        }
    }
}

So in your code you can say:

public bool IsDoorOpen
{
    get { return isDoorOpen;}
    private set
    {
        isDoorOpen = value;
        DoorsChangeState.FireEvent(this);
    }
}
like image 26
Oliver Avatar answered Nov 03 '22 14:11

Oliver