C# 如何对不同的时间间隔使用相同的计时器?
我在代码中使用计时器。单击相应按钮时,状态栏在勾号事件中更新,时间间隔如属性中所述,为1秒。现在我想在不同的时间间隔使用相同的计时器,比如说在不同的操作中使用两秒钟。如何实现这一点?创建第二个计时器。破解第一个计时器没有什么好处C# 如何对不同的时间间隔使用相同的计时器?,c#,winforms,C#,Winforms,我在代码中使用计时器。单击相应按钮时,状态栏在勾号事件中更新,时间间隔如属性中所述,为1秒。现在我想在不同的时间间隔使用相同的计时器,比如说在不同的操作中使用两秒钟。如何实现这一点?创建第二个计时器。破解第一个计时器没有什么好处 正如@Henk所指出的,计时器并没有那么贵。(尤其是与修复难以维护的代码相比!)更改计时器间隔属性。我同意@Henk和其他人的观点 但是,这样的方法还是可行的: 例子 更新示例 在每个经过的时间中更改Interval属性。例如,该程序处理数据30秒,睡眠10秒 stat
正如@Henk所指出的,计时器并没有那么贵。(尤其是与修复难以维护的代码相比!)更改计时器间隔属性。我同意@Henk和其他人的观点 但是,这样的方法还是可行的: 例子 更新示例
在每个经过的时间中更改Interval属性。例如,该程序处理数据30秒,睡眠10秒
static class Program
{
private System.Timers.Timer _sleepTimer;
private bool _isSleeping = false;
private int _processTime;
private int _noProcessTime;
static void Main()
{
_processTime = 30000; //30 seconds
_noProcessTime = 10000; //10 seconds
this._sleepTimer = new System.Timers.Timer();
this._sleepTimer.Interval = _processTime;
this._sleepTimer.Elapsed += new System.Timers.ElapsedEventHandler(sleepTimer_Elapsed);
ProcessTimer();
this._sleepTimer.Start();
}
private void sleepTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ProcessTimer();
}
private void ProcessTimer()
{
_sleepTimer.Enabled = false;
_isSleeping = !_isSleeping;
if (_isSleeping)
{
_sleepTimer.Interval = _processTime;
//process data HERE on new thread;
}
else
{
_sleepTimer.Interval = _noProcessTime;
//wait fired thread and sleep
Task.WaitAll(this.Tasks.ToArray());
}
_sleepTimer.Enabled = true;
}
}
首先想到的问题是为什么?你可以一起破解一个解决方案,但问题是为什么?对。计时器没有那么贵。但是一个计时器滴答声事件是否可能会干扰另一个计时器事件。它们有可能同时被触发。@ghd:当然,两个计时器事件几乎可以同时触发。这是个什么问题?@ghd:那么@DecCyclone建议的代码可能是可行的方法,但这是一个不寻常的要求。我希望同一个计时器有两个不同的时间间隔。但是,正如你已经知道的,这不是一个好的做法。这只是为了满足你的好奇心。我有一个计时器。但它开始晚了一秒钟。我是说,我是根据计时器计时的。但是时钟中的时间比实际经过的时间少一秒。计时器启动晚了一秒钟。如何将我的计时器设置为在正确的时间启动以显示实际时间?当您启动计时器时,它将在您设置的间隔后调用它的第一个
Tick
事件。在您的情况下,您必须将其设置为1秒。这就是为什么它在1秒后启动(调用其勾选事件)。我已经把这件作品到处张贴了。
private void Form_Load()
{
timer1.Interval = 1000; // 1 second
timer1.Start(); // This will raise Tick event after 1 second
OnTick(); // So, call Tick event explicitly when we start timer
}
Int32 counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
OnTick();
}
private void OnTick()
{
if (counter % 1 == 0)
{
OnOneSecond();
}
if (counter % 2 == 0)
{
OnTwoSecond();
}
counter++;
}
static class Program
{
private System.Timers.Timer _sleepTimer;
private bool _isSleeping = false;
private int _processTime;
private int _noProcessTime;
static void Main()
{
_processTime = 30000; //30 seconds
_noProcessTime = 10000; //10 seconds
this._sleepTimer = new System.Timers.Timer();
this._sleepTimer.Interval = _processTime;
this._sleepTimer.Elapsed += new System.Timers.ElapsedEventHandler(sleepTimer_Elapsed);
ProcessTimer();
this._sleepTimer.Start();
}
private void sleepTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ProcessTimer();
}
private void ProcessTimer()
{
_sleepTimer.Enabled = false;
_isSleeping = !_isSleeping;
if (_isSleeping)
{
_sleepTimer.Interval = _processTime;
//process data HERE on new thread;
}
else
{
_sleepTimer.Interval = _noProcessTime;
//wait fired thread and sleep
Task.WaitAll(this.Tasks.ToArray());
}
_sleepTimer.Enabled = true;
}
}