Question:
new Program().MainAsync().GetAwaiter().GetResult();
i’ve tried to google every parts of the statement and here’s what i got, so the youtuber i watched wants to call the non-static MainAsync in the static Main() and the GetAwaiter().GetResult() will wait for the MainAsync method to be completed.But i don’t really understand the await Task.Delay(-1) and the GetAwaiter(), hope someone can tell me why he uses GetResult() with await Task.Delay(-1) and what’s GetAwaiter(), thanks.
Answer:
Task.Delay(-1)
blocks the process from exitingTask.GetAwaiter().GetResult()
is equivalent to await
task and is used when the method cannot be marked with async
, in this case, the Main
method. this is not recommended read here for moreIf you have better answer, please add a comment about this, thank you!