Ananya Agarwal
Ananya Agarwal

Reputation: 21

S4462 issue: Calls to "async" methods should not be blocking. How to call asynchronous method from synchronous method?

I am trying to call an asynchronous method from a synchronous method.

For instance: Let methodAsync be an asynchronous method.

static void methodSync()
{
    object1.methodAsync().Wait();
    object1.methodAsync().Result;
    object1.methodAsync().GetAwaiter().GetResult();
}

Suggestion by sonar:

Replace this use of Task.Wait with await.

Similarly, for the Task.Result and GetAwaiter().GetResult() . https://rules.sonarsource.com/csharp/RSPEC-4462

what can we do to call asynchronous method from a synchronous method?

Upvotes: 2

Views: 589

Answers (0)

Related Questions