private void Work() { if (this.startAction != null) { try { this.startAction(); } catch (Exception exception) { Fault = exception; State = PeriodicTaskState.Faulted; } } while (State == PeriodicTaskState.Running) { try { this.periodicAction(); Thread.Sleep(Delay); } catch (Exception exception) { Fault = exception; if (this.faultAction != null) { this.faultAction(); } State = PeriodicTaskState.Faulted; } } if (this.stopAction != null) { try { this.stopAction(); } catch (Exception exception) { Fault = exception; State = PeriodicTaskState.Faulted; } } }
public void Start() { switch (State) { case PeriodicTaskState.Ready: State = PeriodicTaskState.Running; this.thread = new Thread(Work); this.thread.Start(); break; default: throw new InvalidOperationException("Cannot start in this state."); } }
public void Stop() { switch (State) { case PeriodicTaskState.Running: State = PeriodicTaskState.Stopping; this.thread.Join(); State = PeriodicTaskState.Stopped; break; case PeriodicTaskState.Stopping: this.thread.Join(); State = PeriodicTaskState.Stopped; break; case PeriodicTaskState.Stopped: case PeriodicTaskState.Faulted: break; default: throw new InvalidOperationException("Cannot stop in this state."); } }