armin armin - 3 months ago 33
C# Question

How to add event handler in c#?

I'm totally new to Events and delegates in C#.
I want to handle a data read event in one class (ex. program) and port reading is done in another class(ex. transfer).
I know how to do it with delegates alone,but don't know how to do it in events.

Could you give me a simple sample. Thanks.

Answer

look at this example

    public class TimerManager : INotifyPropertyChanged
    {
    private readonly DispatcherTimer dispatcherTimer;
    private TimeSpan durationTimeSpan;
    private string durationTime = "00:00:00";
    private DateTime startTime;
    private bool isStopped = true;
    readonly TimeSpan timeInterval = new TimeSpan(0, 0, 1);
    public event EventHandler Stopped;

    public TimerManager()
    {
        durationTimeSpan = new TimeSpan(0, 0, 0);
        durationTime = durationTimeSpan.ToString();
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += DispatcherTimerTick;
        dispatcherTimer.Interval = timeInterval;
        dispatcherTimer.IsEnabled = false;
        DefaultStopTime = new TimeSpan(17, 30, 0);

    }

    public TimerManager(TimeSpan defaultStopTime)
        : this()
    {
        DefaultStopTime = defaultStopTime;
    }
    #region Properties

    public TimeSpan ElapsedTime
    {
        get { return durationTimeSpan; }
    }
    public string DurationTime
    {
        get { return durationTime; }
        set
        {
            durationTime = value;
            OnPropertyChanged("DurationTime");
        }
    }

    public DateTime StartTime
    {
        get { return startTime; }

    }
    public bool IsTimerStopped
    {
        get
        {
            return isStopped;
        }
        set
        {
            isStopped = value;
            OnPropertyChanged("IsTimerStopped");
        }
    }

    public TimeSpan DefaultStopTime { get; set; }

    #endregion

    #region Start Stop Timer

    public void StartTimer()
    {
        dispatcherTimer.Start();
        durationTimeSpan = new TimeSpan(0,0,0);
        startTime = DateTime.Now;
        IsTimerStopped = false;
    }

    public void StopTimer()
    {
        dispatcherTimer.Stop();
        IsTimerStopped = true;
        if (Stopped != null)
        {
            Stopped(this, new EventArgs());
        }
    }

    #endregion


    public void DispatcherTimerTick(object sender, EventArgs e)
    {
       // durationTimeSpan = DateTime.Now - startTime;
       durationTimeSpan = durationTimeSpan.Add(timeInterval);
        DurationTime = string.Format("{0:d2}:{1:d2}:{2:d2}", durationTimeSpan.Hours, durationTimeSpan.Minutes,
                                     durationTimeSpan.Seconds);
        if (DateTime.Now.TimeOfDay >= DefaultStopTime)
        {
            StopTimer();
        }

    }


}

in this class we have the Timer Stopped event

        public event EventHandler Stopped;

in the Method we call the event handler if it is not null

public void StopTimer()
{
    dispatcherTimer.Stop();
    IsTimerStopped = true;
    if (Stopped != null)
    {
       //call event handler
        Stopped(this, new EventArgs());
    }
}

for use this class and timer stop event look at the code

     var timer = new TimerManager();
     timer.Stopped += TimerStopped;
     void TimerStopped(object sender, EventArgs e)
     {
        // do you code
     }
Comments