private void StopCallback(string reason) { if (_state == AsyncClassState.Faulted) { Logger.LogDebug("StopCallback: Ignoring due to Faulted state!"); return; } if (_state == AsyncClassState.Stopped) { return; } try { if (_state != AsyncClassState.Running) { Logger.LogDebug("StopCallback: Ignoring due to state is not Running!"); return; } AsyncClassState oldState = _state; _state = AsyncClassState.Stopped; if (_state != oldState) { NotifyStateHandlers(oldState, _state, ""); } } catch (Exception excp) { HandleException(excp); } }
private void ResetCallback(object notUsed) { if (_state == AsyncClassState.Faulted) { Logger.LogDebug("ResetCallback: Ignoring due to Faulted state!"); return; } try { if (_state != AsyncClassState.Stopped) { throw new InvalidOperationException("Not stopped!"); } AsyncClassState oldState = _state; _state = AsyncClassState.Initial; if (_state != oldState) { NotifyStateHandlers(oldState, _state, ""); } } catch (Exception excp) { HandleException(excp); } }
public AsyncStateParams(AsyncClassState oldState, AsyncClassState newState, string reason, TContext context) { OldState = oldState; NewState = newState; Context = context; Reason = reason; }
private void HandleException(Exception excp) { // ignore secondary exceptions if (_state == AsyncClassState.Faulted) { Logger.LogDebug("HandleException: Ignoring due to Faulted state: {0}", excp); return; } // notify asynchronous delegates if (AsyncErrorHandlers != null) { _userDispatcher.DispatchCall(new AsyncErrorParams(excp, _context), AsyncErrorCallback); } // handle the exception try { _state = AsyncClassState.Faulted; OnAsyncException(excp); ErrorHandlers?.Invoke(excp, _context); } catch { // we tried our best - ignore Logger.Log(excp); } }
private void NotifyStateHandlers(AsyncClassState oldState, AsyncClassState newState, string reason) { // notify asynchronous delegates if (AsyncStateHandlers != null) { _userDispatcher.DispatchCall(new AsyncStateParams(oldState, newState, reason, _context), AsyncStateCallback); } try { // call generic state change handler OnAsyncStateChanged(oldState, newState, reason); // call specific state change handlers switch (newState) { case AsyncClassState.Initial: OnAsyncStateInitial(oldState, reason); break; case AsyncClassState.Running: OnAsyncStateRunning(oldState, reason); break; case AsyncClassState.Stopped: OnAsyncStateStopped(oldState, reason); break; default: throw new InvalidOperationException("Unknown state: " + newState); } StateHandlers?.Invoke(oldState, newState, reason, _context); } catch (Exception excp) { HandleException(excp); } }
protected virtual void OnAsyncStateStopped(AsyncClassState oldState, string reason) { }
protected virtual void OnAsyncStateRunning(AsyncClassState oldState, string reason) { }
protected virtual void OnAsyncStateInitial(AsyncClassState oldState, string reason) { }
protected virtual void OnAsyncStateChanged(AsyncClassState oldState, AsyncClassState newState, string reason) { }