void _time_Tick(object sender, EventArgs e) { TimerErrorException tex = null; try { var t = (DispatcherTimer)sender; if (!t.IsEnabled) { return; } t.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { t.Stop(); })); IsRunning = true; if (Command != null) { Command(); } } catch (Exception exc) { if (Error != null) { Error(this, tex = new TimerErrorException(exc)); } } finally { IsRunning = false; if (tex == null || !tex.CancelFinishedEvent) { RaiseFinished(); } } }
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); }