public static Exception AlreadyDisposedOrDisposing(DisposalState disposalState = DisposalState.Disposed) { switch (disposalState) { case DisposalState.Disposing: return(new ObjectDisposedException("unknown", "The object is disposing.")); case DisposalState.Disposed: return(new ObjectDisposedException("unknown", "The object is already disposed.")); default: return(new InvalidOperationException($"Invalid disposal state: {disposalState}.")); } }
public virtual void Dispose(bool disposing) { #if DEEP_DEBUG Console.WriteLine("START DISPOSING OF TASK {0} {1}", _invocationThread.Name, DateTime.Now.Subtract(_callStart).TotalSeconds); #endif lock (_lock) { // make sure this kind of thing doesn't happen twice. if (_disposalState > DisposalState.None) { return; } _disposalState = DisposalState.Disposing; } if (disposing) { // Ensure we're cancelled first. Cancel(); if (_actionState < ActionState.Aborting) { // if we're not already in the process of aborting, we'll kick that off in a few seconds. #if DEEP_DEBUG Console.WriteLine("GIVING 5 seconds to die for TASK {0} {1}", _invocationThread.Name, DateTime.Now.Subtract(_callStart).TotalSeconds); #endif _timer.Change(5000, System.Threading.Timeout.Infinite); } else { // stop timer activity DisposeTimer(); } // for all intents, we're completed...even if the abort will run after this. _completed.Set(); _disposalState = DisposalState.Disposed; if (_actionState >= ActionState.Completed) { _completed.Dispose(); _cancellationTokenSource.Dispose(); } } #if DEEP_DEBUG Console.WriteLine("DONE TASK {0} {1}", _invocationThread.Name, DateTime.Now.Subtract(_callStart).TotalSeconds); #endif }
public virtual void Dispose(bool disposing) { lock (_lock) { // make sure this kind of thing doesn't happen twice. if (_disposalState > DisposalState.None) { return; } _disposalState = DisposalState.Disposing; } if (disposing) { // Ensure we're cancelled first. Cancel(); if (_actionState < ActionState.Aborting) { // if we're not already in the process of aborting, we'll kick that off in a few seconds. _timer.Change(5000, System.Threading.Timeout.Infinite); } else { // stop timer activity DisposeTimer(); } // for all intents, we're completed...even if the abort will run after this. _completed.Set(); _disposalState = DisposalState.Disposed; if (_actionState >= ActionState.Completed) { _completed.Dispose(); _cancellationTokenSource.Dispose(); } } }