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

时间:2011-09-29 12:28:55

标签: c# events delegates

我对C#中的事件和代表完全不熟悉。 我想在一个类(例如程序)中处理数据读取事件,并且在另一个类(例如传输)中完成端口读取。 我知道如何单独与代表一起做,但不知道如何在活动中做到这一点。

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

2 个答案:

答案 0 :(得分:3)

看一下这个例子

    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;
在方法中

我们调用事件处理程序,如果它不是null

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
     }

答案 1 :(得分:-1)

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

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

  ...
}