.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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -