直接使用System.Threading.Timer
示例代码:
在程序中调用Start() ,定时器到指定时间时就会弹出对话框提示!
- private System.Threading.Timer timer;
- private bool _status = false;
- private void Start()
- {
- System.Threading.TimerCallback timerCallback = new System.Threading.TimerCallback(_clearTimeoutLocks);
- timer = new System.Threading.Timer(timerCallback, this, 10000, 10000);
- }
- private void _clearTimeoutLocks(object caller)
- {
- if (_status)
- return;
- else
- {
- _status = true;
- try
- {
- //do something here
- MessageBox.Show("is timer!");
- }
- catch (Exception err) { }
- finally
- {
- _status = false;
- }
- }
- }
_status 是我自己定义的一个控制锁, 是为了防止第一次没有执行完的情况下第二次又进来