2

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?

3
  • 1
    It already tells you what to do. What question are you asking? Commented May 26, 2020 at 6:48
  • Replacing Task.Wait with await cannot be done as the methodSync()- the calling function, is a synchronous method trying to call an asnchronous method - methodAsync(). How to call asynchronous method from synchronous method? Commented May 26, 2020 at 7:02
  • 1
    Then you ignore the warning and move on. Commented May 26, 2020 at 7:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.