C
C#2w ago
光年

An async function called without await

when i remove await before new Test1().run1() ,why the app output like that ? why
test1 run1 called!
test1 run2 called!
test1 run1 called!
test1 run2 called!
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()
No description
No description
11 Replies
Jimmacle
Jimmacle2w ago
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
qqdev
qqdev2w ago
Would await Task.Yield() help here?
Jimmacle
Jimmacle2w ago
that should force it yeah, but i don't know if that will actually affect the output order
光年
光年2w ago
why it not stop at await run2() ?
Jimmacle
Jimmacle2w ago
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
光年
光年2w ago
it just an experiment
Jimmacle
Jimmacle2w ago
$async
MODiX
MODiX2w ago
For an introduction to async and await in C#, https://blog.stephencleary.com/2012/02/async-and-await.html
Async 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.
Jimmacle
Jimmacle2w ago
read this
光年
光年2w ago
with await Task.Yield() it worked as expected
No description
No description
光年
光年2w ago
thanks a lot