0

I'm wondering what the behavior is if I call an async method that doesn't await an asynchronos task. For example, suppose the async method has two code paths depending on its parameters, where one code path calls an async method that actually needs to run asynchronously and the other code path doesn't.

When I call such a method, and assuming the second code path is taken, will my thread still be suspended and resumed, or will it just execute as if the method were synchronous?

This appears to be making some difference in parts of my app that update UI elements on the main thread.

Thanks, Frank

Frank LaRosa
  • 3,533
  • 6
  • 26
  • 32
  • 1
    The method will be executed synchronously as if it were a non async method – NGambit Jan 21 '20 at 01:20
  • This seems a bit different than the question this was marked as a duplicate of, so it's worth noting: The main issue here is the return type. If you call an asynchronous method then it will return a `Task` or `Task<>`. So you'll immediately get the `Task` back—exactly as you would with `await`—prior to the rest of the asynchronous function executing. If you need to get the return value, you'll need to call `Wait()` on that `Task` and then extract the underlying value using `Result`—which is basically what `await` does for you. – Jeremy Caney Jan 21 '20 at 01:28
  • 1
    @JeremyCaney You don't need to call Wait() for a Task as accessing the Result property will already wait until the task completes. Wait() is meant when you don't care for the Result value or you have a non-generic task that has no result value. – ckuri Jan 21 '20 at 08:10
  • @ckuri: Aha, thank you for calling that out! – Jeremy Caney Jan 21 '20 at 21:02

0 Answers0