示例#1
0
 public void Init(CommandDelegate command, TimeSpan delay)
 {
     Cancel();
     if (delay != TimeSpan.MinValue)
     {
         Delay = delay;
     }
     _timer = new Timer(o => {
         if (IsRunning)
         {
             return;
         }
         TimerErrorException tex = null;
         try {
             WaitHandler.Reset();
             IsRunning = true;
             command();
             if (_period == infinity)
             {
                 Cancel();
             }
         } catch (Exception exc) {
             if (Error != null)
             {
                 Error(this, tex = new TimerErrorException(exc));
             }
         } finally {
             IsRunning = false;
             try {
                 WaitHandler.Set();
             } catch { }
             if (tex == null || !tex.CancelFinishedEvent)
             {
                 RaiseFinished();
             }
         }
     },
                        null, Delay, _period);
 }