C
C#6mo 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
Jimmacle6mo 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
qqdev6mo ago
Would await Task.Yield() help here?
Jimmacle
Jimmacle6mo ago
that should force it yeah, but i don't know if that will actually affect the output order
光年
光年OP6mo ago
why it not stop at await run2() ?
Jimmacle
Jimmacle6mo 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
光年
光年OP6mo ago
it just an experiment
Jimmacle
Jimmacle6mo ago
$async
MODiX
MODiX6mo 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
Jimmacle6mo ago
read this
光年
光年OP6mo ago
with await Task.Yield() it worked as expected
No description
No description
光年
光年OP6mo ago
thanks a lot
Want results from more Discord servers?
Add your server