日期:2014-05-18 浏览次数:20986 次
public delegate void TimingEventHandler(object sender, TimingEventArgs e); public sealed class TimingController { // 省略单例模式代码 MachineParameters Machines = MachineParameters.GetInstance(); private Timer TimerGlobal; public event TimingEventHandler WarmedUp; private TimingController() { //定时器初始化 } public void Start() { TimerGlobal.Enabled = true; } void TimerGlobal_Elapsed(object sender, ElapsedEventArgs e) { for (int i = 1; i < 8; i++) { Machines.M[i].ClockStep += 100; TimingEventArgs e = new TimingEventArgs(Machines.M[i].ClockStep); CheckWarmedUp(Machines.M[i], e); } } private void CheckWarmedUp(object sender, TimingEventArgs e) { Machine machine = (Machine)sender; if (machine.ClockStep == machine.WarmUpTime) { //UpdateData.UpdateMachine(sender, e); ### WarmedUp(sender, e); } } void OnWarmedUp(object sender, TimingEventArgs e) { if (WarmedUp != null) { WarmedUp(sender, e); } } public sealed class UpdateData { // 省略单例模式代码 public void UpdateMachine(object sender, TimingEventArgs e) { Machine machine = (Machine)sender; *** Trace.WriteLine(id.ToString() + "------" + e.ClockRate.ToString()); } } public class Machine //实例类 { TimingController Timing = TimingController.GetInstance(); UpdateData Update = UpdateData.GetInstance(); //省略其余定义 //类中用到了 TimingController 和 UpdateData ,因此声明如上 public Machine(int id) { Timing.WarmedUp += new TimingEventHandler(Update.UpdateMachine); } }