2011-09-29 62 views
0

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

你能给我一个简单的例子。谢谢。

+5

我敢肯定,微软样本此。 – BoltClock

+2

嗨 - 正常情况下,你只是“处理程序”添加到像'Button.Click + = OnButtonClickedHandler;'这样的事件 - 但是你用什么方式(例如程序)?你想处理进程/程序中的事件吗? – Carsten

回答

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

    } 


} 

这节课,我们已在停止计时器事件

 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

如果我nright明白你问什么,你可以做这样的事情:

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

    ... 
} 
相关问题