.net - C# await task Console splash -
this question has answer here:
static void main(string[] args) { print(); } static async void print() { try { await task.factory.startnew(() => { thread.sleep(3000); console.writeline("3"); debug.write("3"); }); } catch (exception) { } console.read(); }
console splashed without error occurs!
it happens because print
method called in parallel main
goes on , since there nothing else do, returns. after main
finished program exits.
if want wait print
method, change returns task
instead of void
, wait task in main
method:
static void main(string[] args) { print().wait(); } static async task print() {...}
Comments
Post a Comment