c# - await/async odd behaviour -
i have simple console application
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace consoleapplication2 { public class program { static void main(string[] args) { mockapi api = new mockapi(); task t = api.getversion(); var placebreakpoint = "breakpoint"; } } public class mockapi { public async task<string> getversion() { return await apiversion(); } private async task<string> apiversion() { await task.delay(3000); return "v1.0"; } } } when ran first time executed expect seeing code go way
await task.delay(3000); and returning to
var placebreakpoint = "breakpoint"; before returning
return "v1.0"; when delay had been completed. running code thereafter sees code execute before never return task.delay. i'm missing fundamental here.
you not waiting task application ends before task has chance complete.
you need wait or await (which can't in main should anywhere else).
static void main(string[] args) { mockapi api = new mockapi(); task t = api.getversion(); var placebreakpoint = "breakpoint"; t.wait(); }
Comments
Post a Comment