c#实现一个定时器
c# implementing a timer
我在实施 timer
方面比我应该做的更多,所以我决定 post 在这里提问。
这是我的 class:
public static class CurrentPoint
{
public static Single X { get; set; }
public static Single Y { get; set; }
public static Single Z { get; set; }
public static long ID { get; set; }
public static float CalcVar { get; set; }
public static float CalcStatic { get; set; }
public static bool StartTimeOut()
{
return true;
}
}
我应该在其中实现一个方法 StartTimeOut()
。 StartTimeOut()
将在其他方法执行时从另一个 class 调用。
在 StartTimeOut()
我应该有一个 timer
来检查 CalcVar
是否会在 接下来的 30 秒内改变 .
现在,如果可以,我将从我的 StartTimeOut()
收到一个 TRUE
并且 timer
应该退出,否则 StartTimeOut()
将 return 一个 false
.
CalcVar
的检查将在相同的 CurrentPoint.ID
下完成。这意味着如果 ID
在定时器检查期间发生变化,定时器应该退出并且 StartTimeOut()
将 return a TRUE
.
还应该检查计时器是否已经 运行,如果 CalcVar 在同一 ID 下 30 秒内达到 0,则再次停止计时器
和 StartTimeOut() 再次 returns TRUE。
我希望我没有把这个问题弄得太复杂。
我创建了一个小示例,请理解无论何时调用此函数,只要它是 运行,它就会一直留在 while 循环中。也许您应该从另一个线程中调用 StartTimeOut() 函数...
//do not forget
using System.Timers;
private Timer _timer;
private static long _id;
private static bool _returnValue;
private static int _achievedTime;
public static bool StartTimeOut()
{
//set your known values (you need to check these values over time
_id = ID;
_achievedTime = 0;
_returnValue = true;
//start your timer
Start();
while(_timer.Enabled)
{
//an empty while loop that will run as long as the timer is enabled == running
}
//as soon as it stops
return _returnValue;
}
//sets the timer values
private static void Start()
{
_timer = new Times(100); //describes your interval for every 100ms
_timer.Elapsed += HandleTimerElapsed;
_timer.AutoReset = true; //will reset the timer whenever the Elapsed event is called
//start your timer
_timer.Enabled = true; //Could also be _timer.Start();
}
//handle your timer event
//Checks all your values
private static void HandleTimerElapsed(object sender, ElapsedEventArgs e)
{
_achievedTime += _timer.Interval;
//the ID changed => return true and stop the timer
if(ID!= _id)
{
_returnValue = true;
_timer.enabled = false; //Could also be _timer.Stop();
}
if(CalcVar == 0) //calcVar value reached 0 => return false and stop the timer
{
_returnValue = false;
_timer.Enabled = false;
}
if(_achievedTime = 30000) //your 30s passed, stop the timer
_timer.Enabled = false;
}
这就是你所谓的简单代码不要测试!
我在实施 timer
方面比我应该做的更多,所以我决定 post 在这里提问。
这是我的 class:
public static class CurrentPoint
{
public static Single X { get; set; }
public static Single Y { get; set; }
public static Single Z { get; set; }
public static long ID { get; set; }
public static float CalcVar { get; set; }
public static float CalcStatic { get; set; }
public static bool StartTimeOut()
{
return true;
}
}
我应该在其中实现一个方法 StartTimeOut()
。 StartTimeOut()
将在其他方法执行时从另一个 class 调用。
在 StartTimeOut()
我应该有一个 timer
来检查 CalcVar
是否会在 接下来的 30 秒内改变 .
现在,如果可以,我将从我的 StartTimeOut()
收到一个 TRUE
并且 timer
应该退出,否则 StartTimeOut()
将 return 一个 false
.
CalcVar
的检查将在相同的 CurrentPoint.ID
下完成。这意味着如果 ID
在定时器检查期间发生变化,定时器应该退出并且 StartTimeOut()
将 return a TRUE
.
还应该检查计时器是否已经 运行,如果 CalcVar 在同一 ID 下 30 秒内达到 0,则再次停止计时器 和 StartTimeOut() 再次 returns TRUE。
我希望我没有把这个问题弄得太复杂。
我创建了一个小示例,请理解无论何时调用此函数,只要它是 运行,它就会一直留在 while 循环中。也许您应该从另一个线程中调用 StartTimeOut() 函数...
//do not forget
using System.Timers;
private Timer _timer;
private static long _id;
private static bool _returnValue;
private static int _achievedTime;
public static bool StartTimeOut()
{
//set your known values (you need to check these values over time
_id = ID;
_achievedTime = 0;
_returnValue = true;
//start your timer
Start();
while(_timer.Enabled)
{
//an empty while loop that will run as long as the timer is enabled == running
}
//as soon as it stops
return _returnValue;
}
//sets the timer values
private static void Start()
{
_timer = new Times(100); //describes your interval for every 100ms
_timer.Elapsed += HandleTimerElapsed;
_timer.AutoReset = true; //will reset the timer whenever the Elapsed event is called
//start your timer
_timer.Enabled = true; //Could also be _timer.Start();
}
//handle your timer event
//Checks all your values
private static void HandleTimerElapsed(object sender, ElapsedEventArgs e)
{
_achievedTime += _timer.Interval;
//the ID changed => return true and stop the timer
if(ID!= _id)
{
_returnValue = true;
_timer.enabled = false; //Could also be _timer.Stop();
}
if(CalcVar == 0) //calcVar value reached 0 => return false and stop the timer
{
_returnValue = false;
_timer.Enabled = false;
}
if(_achievedTime = 30000) //your 30s passed, stop the timer
_timer.Enabled = false;
}
这就是你所谓的简单代码不要测试!