public void SetAsCompleted(Exception exception, bool completedSynchronously)
{
// Passing null for exception means no error occurred; this is the common case
_exception = exception;
// The m_CompletedState field MUST be set prior calling the callback
var prevState = Interlocked.Exchange(ref _completedState,
completedSynchronously ? StateCompletedSynchronously : StateCompletedAsynchronously);
if (prevState != StatePending)
throw new InvalidOperationException("You can set a result only once");
// If the event exists, set it
if (_asyncWaitHandle != null)
_asyncWaitHandle.Set();
// If a callback method was set, call it
if (_asyncCallback != null)
_asyncCallback(this);
}