/// <summary> /// Raises the operation timed out event. /// </summary> /// <param name="e"> /// The event arguments. /// </param> protected virtual void OnOperationTimedOut(FireplaceTimeoutEventArgs e) { if (this.OperationTimedOut != null) { this.OperationTimedOut(this, e); } }
/// <summary> /// Sets the timeout delay. /// </summary> /// <param name="delay"> /// The timeout delay. /// </param> /// <param name="unit"> /// The time unit of measure for the timeout. /// </param> public void SetTimeout(double delay, TimeUnit unit) { if (this.IsOff) { throw new InvalidOperationException("Cannot set timeout when fireplace is off."); } this._timeoutDelay = delay; this._timeoutUnit = unit; this.CancelTimeoutTask(); if (this._timeoutDelay > 0) { TimeSpan waitTime = TimeSpan.Zero; switch (unit) { case TimeUnit.Days: waitTime = TimeSpan.FromDays(delay); break; case TimeUnit.Hours: waitTime = TimeSpan.FromHours(delay); break; case TimeUnit.Minutes: waitTime = TimeSpan.FromMinutes(delay); break; case TimeUnit.Seconds: waitTime = TimeSpan.FromSeconds(delay); break; case TimeUnit.Milliseconds: waitTime = TimeSpan.FromMilliseconds(delay); break; default: break; } Action a = new Action(delegate { this._token.WaitHandle.WaitOne(waitTime); FireplaceTimeoutEventArgs evt = new FireplaceTimeoutEventArgs(); this.OnOperationTimedOut(evt); if (!evt.IsHandled) { this.Off(); } }); TimeSpan killDelay = TimeSpan.FromTicks(waitTime.Ticks).Add(TimeSpan.FromSeconds(1)); Timer killTimer = new Timer(state => { this._cts.Cancel(); }, null, killDelay.Milliseconds, -1); this._backgroundTask = Task.Factory.StartNew(a); this._backgroundTask.ContinueWith(completed => { killTimer.Dispose(); }); } }
/// <summary> /// Sets the timeout delay. /// </summary> /// <param name="delay"> /// The timeout delay. /// </param> /// <param name="unit"> /// The time unit of measure for the timeout. /// </param> public void SetTimeout(double delay, TimeUnit unit) { if (this.IsOff) { throw new InvalidOperationException("Cannot set timeout when fireplace is off."); } this._timeoutDelay = delay; this._timeoutUnit = unit; this.CancelTimeoutTask(); if (this._timeoutDelay > 0) { TimeSpan waitTime = TimeSpan.Zero; switch (unit) { case TimeUnit.Days: waitTime = TimeSpan.FromDays(delay); break; case TimeUnit.Hours: waitTime = TimeSpan.FromHours(delay); break; case TimeUnit.Minutes: waitTime = TimeSpan.FromMinutes(delay); break; case TimeUnit.Seconds: waitTime = TimeSpan.FromSeconds(delay); break; case TimeUnit.Milliseconds: waitTime = TimeSpan.FromMilliseconds(delay); break; default: break; } Action a = new Action(delegate { this._token.WaitHandle.WaitOne(waitTime); FireplaceTimeoutEventArgs evt = new FireplaceTimeoutEventArgs(); this.OnOperationTimedOut(evt); if (!evt.IsHandled) { this.Off(); } }); TimeSpan killDelay = TimeSpan.FromTicks(waitTime.Ticks).Add(TimeSpan.FromSeconds(1)); Timer killTimer = new Timer(state => { this._cts.Cancel(); }, null, killDelay.Milliseconds, -1); this._backgroundTask = Task.Factory.StartNew(a); this._backgroundTask.ContinueWith(completed => { killTimer.Dispose(); }); } }
/// <summary> /// Raises the operation timed out event. /// </summary> /// <param name="e"> /// The event arguments. /// </param> protected virtual void OnOperationTimedOut(FireplaceTimeoutEventArgs e) { if (this.OperationTimedOut != null) { this.OperationTimedOut(this, e); } }