Renci.SshNet.Common.AsyncResult.SetAsCompleted C# (CSharp) Method

SetAsCompleted() public method

Marks asynchronous operation as completed.
public SetAsCompleted ( Exception exception, bool completedSynchronously ) : void
exception System.Exception The exception.
completedSynchronously bool if set to true [completed synchronously].
return void
        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);
        }