The Static Analysis tool detects whether async/await keywords are redundant.
You can omit async/await keywords if a method does not have the continuation code block. This allows you to avoid unnecessary delay in code execution. For example, you can return a Task from a method instead of the task's result if it is a Task type method.
async Task<string> DemoMethod(bool value, CancellationToken token) {
if (value)
return await Task.Run(() => "result1", token).ConfigureAwait(false); // CRR0030
return await Task.Run(() => "result2", token).ConfigureAwait(false); // CRR0030
}
Async Function DemoMethod(value As Boolean, token As CancellationToken) As Task(Of String)
If value Then
Return Await Task.Run(Function() "result1", token).ConfigureAwait(False) ' CRR0030
End If
Return Await Task.Run(Function() "result2", token).ConfigureAwait(False) ' CRR0030
End Function
Change the code above as follows to omit async/await:
Task<string> DemoMethod(bool value, CancellationToken token) {
if (value)
return Task.Run(() => "result1", token);
return Task.Run(() => "result2", token);
}
Private Function DemoMethod(value As Boolean, token As CancellationToken) As Task(Of String)
If value Then
Return Task.Run(Function() "result1", token)
End If
Return Task.Run(Function() "result2", token)
End Function
If your async method contains the continuation code you cannot omit async/await keywords:
async Task<string> DemoMethodAsync(bool value, CancellationToken token) {
if (value)
return await SomeMethodWhichReturnedTask().ConfigureAwait(false);
var someValue = await SomeAsyncMethod().ConfigureAwait(false);
if (someValue)
DoSomething();
return await AnotherMethodWhichReturnedTask().ConfigureAwait(false);
}
Private Async Function DemoMethodAsync(ByVal value As Boolean, ByVal token As CancellationToken) As Task(Of String)
If value Then Return Await SomeMethodWhichReturnedTask().ConfigureAwait(False)
Dim someValue = Await SomeAsyncMethod().ConfigureAwait(False)
If someValue Then DoSomething()
Return Await AnotherMethodWhichReturnedTask().ConfigureAwait(False)
End Function
In the code above, when thread reaches await SomeAsyncMethod() this await makes the compiler to run operation on a new task and waits while this task is completed. Then, await causes thread to return and continue with execution. After the compiler finishes SomeAsyncMethod() it executes the DoSomething() method.
We are updating the DevExpress product documentation website and this page is part of our new experience. During this transition period, product documentation remains available in our previous format at documentation.devexpress.com. Learn More...