简体   繁体   中英

Program exits upon calling await

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 message for me. Here is the (cut-short) code:

private void InitializeMessageSystem ( ) 
{
    do
    {
        // Do stuff
        await printMessage ("Hello World!");
        Console.ReadKey();
    } while (condition != true)
}

And here the function PrintMessage() :

private static async Task PrintMessage (string message, int spd = 1)
{
    int delay = 50 / spd;

    string[] words = message.Split(' ');

    int index = 1;

    for (int word = 0; word < words.Length; word++)
    {
        char[] current = words[word].ToCharArray();
        if (index + current.Length > Console.WindowWidth)
        {
            Console.WriteLine();
            index = 1;
        }
        for (int c = 0; c < current.Length; c++)
        {
            Console.Write(current[c]);
            await Task.Delay(delay);
        }
        Console.Write(" ");
    }
}

Edit : Here's the call from the main function:

static void Main (string[] args) 
{
    InitializeMessageSystem();
    Console.ReadKey();
}

Question

Why does my program exit, when I press a key while the function is not yet completed? I thought the program would wait for the Console.ReadKey() until the function PrintMessage() is completed?

Your problem is that await returns the control flow of the program to the caller of the function. Normally execution is continued at that point when the asynchronous task you await finishes.

So control is returned to your main function as you wait for printMessage and main now waits for a key input. As you hit the key main returns to the OS and your process (including all asynchronous tasks) terminates.

Change your InitializeMessageSystem to

private async Task InitializeMessageSystem ( )  

and change the code in main to

InitializeMessageSystem().Wait();

to wait until InitializeMessageSystem finishes completely before waiting for the key.

The below code executes without any errors or warnings. But when you execute the code the program exits silently. What you might be expecting is the program waits for the task to complete, asks the user to press any key and exit. What actually happens is after the await statement is executed the control goes back to the invoking step. In our case after the step await task; is executed, before the task completes the control goes back to the invoking step SomeTask(); and the program exits.

class Program
{
    static void Main(string[] args)
    {
        SomeTask();
    }

    public static async void SomeTask()
    {
        Task task = Task.Run(() =>
        {
            System.Threading.Thread.Sleep(20000);
            Console.WriteLine("Task Completed!");
        });
        await task;
        Console.WriteLine("Press any key to exit");
        Console.ReadLine();
    }
}

To fix this, add await to the SomeTask(); call so that the program waits for async SomeTask() to complete. You should also change the return type of SomeTask() from void to Task.

class Program
{
    static void Main(string[] args)
    {
        await SomeTask();
    }

    public static async Task SomeTask()
    {
        Task task = Task.Run(() =>
        {
            System.Threading.Thread.Sleep(20000);
            Console.WriteLine("Task Completed!");
        });
        await task;
        Console.WriteLine("Press any key to exit");
        Console.ReadLine();
    }
}

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 <TResult > 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<int> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM