我正在尝试为计时器创建一个抽象层。基本上,这个想法是创建一个抽象两个独立计时器的类。基本上,计时器抽象类应该在“关闭”时触发一个事件,在“打开”时触发另一个事件。因此,例如,如果这是一个交通灯,timer1 将控制绿灯,timer2 将控制红灯(让我们忽略黄色)。
如您所见,timer1_Elapsed并timer2_Elapsed处理计时器的“菊花链”。在这些事件发生后,我想要另一个事件(这个计时器的真正目的)发生。
问题似乎是 myEvent1和Event2 ElapsedEventHandlers 似乎没有按预期工作。我收到以下错误: 'TwoCycleTimer.Event1.get' must declare a body because it is not marked abstract, extern, or partial同样的事情Event2
我应该注意,声明一个 bodyget什么也没做,只会让事情变得更糟。
get {return timer1.Elapsed; };导致 can only appear on the left hand side of += or =错误
using System;
using System.Timers;
public class TwoCycleTimer
{
private Boolean enabled;
private Timer timer1;
private Timer timer2;
public Boolean Enabled
{
get { return enabled; }
set
{
timer1.Enabled = value;
enabled = value;
}
}
public double Interval1
{
get { return timer1.Interval; }
set { timer1.Interval = value; }
}
public double Interval2
{
get { return timer2.Interval; }
set { timer2.Interval = value; }
}
public ElapsedEventHandler Event1 { get; set { timer1.Elapsed += value; } }
public ElapsedEventHandler Event2 { get; set { timer2.Elapsed += value; } }
public TwoCycleTimer(bool Enabled = false, double Interval1 = 4000, double Interval2 = 60000, ElapsedEventHandler Event1 = null, ElapsedEventHandler Event2 = null)
{
timer1 = new Timer(Interval1);
timer2 = new Timer(Interval2);
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
if(Event1 != null)
timer1.Elapsed += Event1;
if(Event2 != null)
timer2.Elapsed += Event2;
this.Enabled = enabled;
}
private void timer1_Elapsed(object source, ElapsedEventArgs e)
{
if (Enabled)
{
timer1.Enabled = false;
timer2.Enabled = true;
}
}
private void timer2_Elapsed(object source, ElapsedEventArgs e)
{
if (Enabled)
{
timer2.Enabled = false;
timer1.Enabled = true;
}
}
}
慕神8447489
相关分类