An async function called without await
when i remove
await
before new Test1().run1()
,why the app output like that ?
why
is output before main after run1 called!
the run1()
func not be await
, why it not be skip ?
and why it stop at Task.Delay(1000)
but not await run2()
11 Replies
because async methods run synchronously until the first await
and since the only await in your code that is actually asynchronous is the Task.Delay, that's where control returns to main
Would
await Task.Yield()
help here?that should force it yeah, but i don't know if that will actually affect the output order
why it not stop at
await run2()
?because it hasn't hit any asynchronous code yet
do you have a real problem you're trying to figure out or is this purely an experiment
it just an experiment
$async
For an introduction to
async
and await
in C#, https://blog.stephencleary.com/2012/02/async-and-await.htmlAsync and Await
Most people have already heard about the new “async” and “await” functionality coming in Visual Studio 11. This is Yet Another Introductory Post.
read this
with
await Task.Yield()
it worked as expectedthanks a lot