Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to not await async method call?

I have an application which will upload files. I don't want my application to halt during the file upload, so I want to do the task asynchronously. I have something like this:

class Program
{
    static void Main(string[] args)
    {
        //less than 5 seconds
        PrepareUpload();
    }

    private static async Task PrepareUpload()
    {
        //processing

        await Upload();

        //processing
    }

    private static Task Upload()
    {
        var task = Task.Factory.StartNew(() => System.Threading.Thread.Sleep(5000));

        return task;
    }
}

The exceptions are being treated internally, so that is not a problem.

Is it okay to use async/away like a shoot and forget like this?

like image 764
Bruno Klein Avatar asked May 03 '13 00:05

Bruno Klein


1 Answers

In a console app, you need to wait. Otherwise, your application will exit, terminating all background threads and asynchronous operations that are in progress.

like image 146
Stephen Cleary Avatar answered Oct 21 '22 11:10

Stephen Cleary