如何在c#中添加事件处理程序

本文关键字:添加 事件处理 程序 | 更新日期: 2023-09-27 18:09:58

我对c#中的事件和委托完全陌生。我想处理一个类中的数据读取事件(例如程序)和端口读取是在另一个类(例如程序)中完成的。转移)。我知道如何单独处理委托,但不知道如何处理事件。

你能给我一个简单的样品吗?谢谢。

如何在c#中添加事件处理程序

看看这个例子

    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();
        }
    }

}

在这个类中我们有Timer Stopped事件

        public event EventHandler Stopped;
方法中的

如果不为空,我们调用事件处理程序

public void StopTimer()
{
    dispatcherTimer.Stop();
    IsTimerStopped = true;
    if (Stopped != null)
    {
       //call event handler
        Stopped(this, new EventArgs());
    }
}
要使用该类和计时器停止事件,请查看代码
     var timer = new TimerManager();
     timer.Stopped += TimerStopped;
     void TimerStopped(object sender, EventArgs e)
     {
        // do you code
     }

如果我正确理解了你的要求,你可以这样做:

public class MyClass
{
  ...
  public  void delegate MuCustomeEventDelegate( ...params...);
  public event MuCustomeEventDelegate MuCustomeEvent;
  ...
}