# < Summary

Information

Class: | Renci.SshNet.Common.AsyncResult
Assembly: | Renci.SshNet
File(s): | \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\AsyncResult.cs

Line coverage
84%

Covered lines: | 42
Uncovered lines: | 8
Coverable lines: | 50
Total lines: | 158
Line coverage: | 84%

Branch coverage
85%

Covered branches: | 17
Total branches: | 20
Branch coverage: | 85%

Method coverage

Feature is only available for sponsors

Upgrade to PRO version

# Metrics

Method | Branch coverage | Cyclomatic complexity | Line coverage
--- | --- | --- | ---
.ctor(...) | 100% | 1 | 100%
get_EndInvokeCalled() | 100% | 1 | 100%
SetAsCompleted(...) | 75% | 8 | 80%
EndInvoke() | 100% | 4 | 100%
get_AsyncState() | 100% | 1 | 100%
get_CompletedSynchronously() | 100% | 1 | 100%
get_AsyncWaitHandle() | 75% | 8 | 66.66%
get_IsCompleted() | 100% | 1 | 100%

# File(s)

## \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\AsyncResult.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Threading;`
|  | `3` |  | ``
|  | `4` |  | `namespace Renci.SshNet.Common`
|  | `5` |  | `{`
|  | `6` |  | `/// <summary>`
|  | `7` |  | `/// Base class to encapsulates the results of an asynchronous operation.`
|  | `8` |  | `/// </summary>`
|  | `9` |  | `public abstract class AsyncResult : IAsyncResult`
|  | `10` |  | `{`
|  | `11` |  | `private const int StatePending = 0;`
|  | `12` |  | ``
|  | `13` |  | `private const int StateCompletedSynchronously = 1;`
|  | `14` |  | ``
|  | `15` |  | `private const int StateCompletedAsynchronously = 2;`
|  | `16` |  | ``
|  | `17` |  | `private readonly AsyncCallback _asyncCallback;`
|  | `18` |  | `private readonly object _asyncState;`
| 4548 | `19` |  | `private int _completedState = StatePending;`
|  | `20` |  | `private ManualResetEvent _asyncWaitHandle;`
|  | `21` |  | `private Exception _exception;`
|  | `22` |  | ``
|  | `23` |  | `/// <summary>`
|  | `24` |  | `/// Initializes a new instance of the <see cref="AsyncResult"/> class.`
|  | `25` |  | `/// </summary>`
|  | `26` |  | `/// <param name="asyncCallback">The async callback.</param>`
|  | `27` |  | `/// <param name="state">The state.</param>`
| 4548 | `28` |  | `protected AsyncResult(AsyncCallback asyncCallback, object state)`
| 4548 | `29` |  | `{`
| 4548 | `30` |  | `_asyncCallback = asyncCallback;`
| 4548 | `31` |  | `_asyncState = state;`
| 4548 | `32` |  | `}`
|  | `33` |  | ``
|  | `34` |  | `/// <summary>`
|  | `35` |  | `/// Gets a value indicating whether <see cref="EndInvoke()"/> has been called on the current <see cref="AsyncRes`
|  | `36` |  | `/// </summary>`
|  | `37` |  | `/// <value>`
|  | `38` |  | `/// <see langword="true"/> if <see cref="EndInvoke()"/> has been called on the current <see cref="AsyncResult"/>`
|  | `39` |  | `/// otherwise, <see langword="false"/>.`
|  | `40` |  | `/// </value>`
| 4553 | `41` |  | `public bool EndInvokeCalled { get; private set; }`
|  | `42` |  | ``
|  | `43` |  | `/// <summary>`
|  | `44` |  | `/// Marks asynchronous operation as completed.`
|  | `45` |  | `/// </summary>`
|  | `46` |  | `/// <param name="exception">The exception.</param>`
|  | `47` |  | `/// <param name="completedSynchronously">If set to <see langword="true"/>, completed synchronously.</param>`
|  | `48` |  | `public void SetAsCompleted(Exception exception, bool completedSynchronously)`
| 4310 | `49` |  | `{`
|  | `50` |  | `// Passing null for exception means no error occurred; this is the common case`
| 4310 | `51` |  | `_exception = exception;`
|  | `52` |  | ``
|  | `53` |  | `// The '_completedState' field MUST be set prior calling the callback`
| 4310 | `54` |  | `var prevState = Interlocked.Exchange(ref _completedState,`
| 4310 | `55` |  | `completedSynchronously ? StateCompletedSynchronously : StateCompletedAs`
|  | `56` |  | ``
| 4310 | `57` |  | `if (prevState != StatePending)`
| 0 | `58` |  | `{`
| 0 | `59` |  | `throw new InvalidOperationException("You can set a result only once");`
|  | `60` |  | `}`
|  | `61` |  | ``
|  | `62` |  | `// If the event exists, set it`
| 4310 | `63` |  | `_ = _asyncWaitHandle?.Set();`
|  | `64` |  | ``
|  | `65` |  | `// If a callback method was set, call it`
| 4310 | `66` |  | `_asyncCallback?.Invoke(this);`
| 4310 | `67` |  | `}`
|  | `68` |  | ``
|  | `69` |  | `/// <summary>`
|  | `70` |  | `/// Waits until the asynchronous operation completes, and then returns.`
|  | `71` |  | `/// </summary>`
|  | `72` |  | `internal void EndInvoke()`
| 4303 | `73` |  | `{`
|  | `74` |  | `// This method assumes that only 1 thread calls EndInvoke for this object`
| 4303 | `75` |  | `if (!IsCompleted)`
| 12 | `76` |  | `{`
|  | `77` |  | `// If the operation isn't done, wait for it`
| 12 | `78` |  | `_ = AsyncWaitHandle.WaitOne();`
| 12 | `79` |  | `_asyncWaitHandle = null; // Allow early GC`
| 12 | `80` |  | `AsyncWaitHandle.Dispose();`
| 12 | `81` |  | `}`
|  | `82` |  | ``
| 4303 | `83` |  | `EndInvokeCalled = true;`
|  | `84` |  | ``
|  | `85` |  | `// Operation is done: if an exception occurred, throw it`
| 4303 | `86` |  | `if (_exception != null)`
| 40 | `87` |  | `{`
| 40 | `88` |  | `throw _exception;`
|  | `89` |  | `}`
| 4263 | `90` |  | `}`
|  | `91` |  | ``
|  | `92` |  | `/// <summary>`
|  | `93` |  | `/// Gets a user-defined object that qualifies or contains information about an asynchronous operation.`
|  | `94` |  | `/// </summary>`
|  | `95` |  | `/// <returns>`
|  | `96` |  | `/// A user-defined object that qualifies or contains information about an asynchronous operation.`
|  | `97` |  | `/// </returns>`
|  | `98` |  | `public object AsyncState`
|  | `99` |  | `{`
| 12090 | `100` |  | `get { return _asyncState; }`
|  | `101` |  | `}`
|  | `102` |  | ``
|  | `103` |  | `/// <summary>`
|  | `104` |  | `/// Gets a value indicating whether the asynchronous operation completed synchronously.`
|  | `105` |  | `/// </summary>`
|  | `106` |  | `/// <returns>`
|  | `107` |  | `/// <see langword="true"/> if the asynchronous operation completed synchronously; otherwise, <see langword="fals`
|  | `108` |  | `/// </returns>`
|  | `109` |  | `public bool CompletedSynchronously`
|  | `110` |  | `{`
| 24 | `111` |  | `get { return _completedState == StateCompletedSynchronously; }`
|  | `112` |  | `}`
|  | `113` |  | ``
|  | `114` |  | `/// <summary>`
|  | `115` |  | `/// Gets a <see cref="WaitHandle"/> that is used to wait for an asynchronous operation to complete.`
|  | `116` |  | `/// </summary>`
|  | `117` |  | `/// <returns>`
|  | `118` |  | `/// A <see cref="WaitHandle"/> that is used to wait for an asynchronous operation to complete.`
|  | `119` |  | `/// </returns>`
|  | `120` |  | `public WaitHandle AsyncWaitHandle`
|  | `121` |  | `{`
|  | `122` |  | `get`
| 232 | `123` |  | `{`
| 232 | `124` |  | `if (_asyncWaitHandle is null)`
| 231 | `125` |  | `{`
| 231 | `126` |  | `var done = IsCompleted;`
| 231 | `127` |  | `var mre = new ManualResetEvent(done);`
| 231 | `128` |  | `if (Interlocked.CompareExchange(ref _asyncWaitHandle, mre, comparand: null) != null)`
| 0 | `129` |  | `{`
|  | `130` |  | `// Another thread created this object's event; dispose the event we just created`
| 0 | `131` |  | `mre.Dispose();`
| 0 | `132` |  | `}`
|  | `133` |  | `else`
| 231 | `134` |  | `{`
| 231 | `135` |  | `if (!done && IsCompleted)`
| 0 | `136` |  | `{`
|  | `137` |  | `// If the operation wasn't done when we created the event but now it is done, set the event`
| 0 | `138` |  | `_ = _asyncWaitHandle.Set();`
| 0 | `139` |  | `}`
| 231 | `140` |  | `}`
| 231 | `141` |  | `}`
|  | `142` |  | ``
| 232 | `143` |  | `return _asyncWaitHandle;`
| 232 | `144` |  | `}`
|  | `145` |  | `}`
|  | `146` |  | ``
|  | `147` |  | `/// <summary>`
|  | `148` |  | `/// Gets a value indicating whether the asynchronous operation has completed.`
|  | `149` |  | `/// </summary>`
|  | `150` |  | `/// <returns>`
|  | `151` |  | `/// <see langword="true"/> if the operation is complete; otherwise, <see langword="false"/>.`
|  | `152` |  | `/// </returns>`
|  | `153` |  | `public bool IsCompleted`
|  | `154` |  | `{`
| 15678 | `155` |  | `get { return _completedState != StatePending; }`
|  | `156` |  | `}`
|  | `157` |  | `}`
|  | `158` |  | `}`

# Methods/Properties

.ctor(System.AsyncCallback,System.Object)
get_EndInvokeCalled()
SetAsCompleted(System.Exception,System.Boolean)
EndInvoke()
get_AsyncState()
get_CompletedSynchronously()
get_AsyncWaitHandle()
get_IsCompleted()