Program exits upon calling await

后端 未结 3 668
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 18:21

I have a while-loop that should repeat the program until a certain condition is met. Inside this loop I call an async function, which prints out a

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 19:03

    The difference between calling a synchronous function and an async function is that when calling the synchronous function you know that when you reach the statement after the call the function is executed completely. When calling an async function, the function is scheduled as a task at the thread pool to be performed when any of the threads in the pool has time for it.

    This gives you time to do other things while one of the threads is performing the task. As soon as you need the result you await for the task to be finished.

    This works only if your function is also async. If you don't make your function async your can't use async-await. Making your function async and the clients of your function also async is the only way to truly use async-await. Remember: all async functions should return Task instead of void and Task> instead of TResult. The only exception is the event handler

    private async void Button1_Clicked(object sender, ...)
    {
        var myTask = SlowMultiplierAsync(4, 3);
        // while myTask is running you can do other things
        // after a while you need the result:
        int taskResult = await myTask;
        Process(taskResult);
    }
    
    private async Task SlowMultiplierAsync(int x, int y)
    {
         // let's take a nap before multiplying
         // do this by awaiting another async function:
         await Task.Delay(TimeSpan.FromSeconds(5));
         return x * y;
    }
    

    If you don't want (or can) make your function async, you can simulate similar behaviour by starting a task using task.Run:

    private void InitializeMessageSystem ( ) {
    do
    {
        // Do stuff
        var myTask = task.Run( () => printMessage ("Hello World!"));
        myTask.Wait();
        Console.ReadKey();
    } while (condition != true)
    

    Although this will make sure that your function won't end before the task is finished, your function will be a synchronous one for your clients. For instance it won't make your UI responsive.

    Eric lippert here on stackoverflow once explained me the difference between asynchronous concurrent. Link to his answer

    Suppose you have to make breakfast. Toast some bread and cook some eggs.

    • Synchronous: start toasting bread. Wait until toasting finished. Start cooking eggs, wait until eggs are cooked.
    • Asynchronous but not concurrent: start toasting bread, and while the bread is toasting start cooking eggs. While the eggs are cooking yo can do other things, like making tea. After a while you wait until the eggs are cooked and wait until the bread is toasted. This is typically async-await.
    • Asynchronous and concurrent: hire a cook to toast the bread, hire a cook to cook the eggs and wait until both cooks are finished. Here the toasting and cooking is done by different threads. It is the most expensive method

提交回复
热议问题