Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 608 Bytes

VSTHRD107.md

File metadata and controls

27 lines (21 loc) · 608 Bytes

VSTHRD107 Await Task within using expression

The C# using statement requires that the used expression implement IDisposable. Because Task<T> implements IDisposable, one may accidentally omit an await operator and Dispose of the Task<T> instead of the T result itself when T derives from IDisposable.

Examples of patterns that are flagged by this analyzer

AsyncSemaphore lck;
using (lck.EnterAsync())
{
    // ...
}

Solution

Add the await operator within the using expression.

AsyncSemaphore lck;
using (await lck.EnterAsync())
{
    // ...
}