Awaiting a non-async method

26,235

Call your method as following:

await Task.Run(() => YourMethod());

When you use the Task.Run method it creates an awaitable task for you.

Share:
26,235
NibblyPig
Author by

NibblyPig

Hello!

Updated on September 17, 2020

Comments

  • NibblyPig
    NibblyPig over 3 years

    I'm thoroughly confused by the whole await / async pattern in C#.

    I have a forms app, and I want to call a method that takes 20 seconds to do a ton of processing. Therefore I want to await it. I thought the correct way was to mark it as async Task but doing this produces a warning because I don't use await anywhere within it.

    A google revealed something about returning a TaskCompletionSource<T> but I don't have a return type, since it's void.

    How can I call this method using await?