Bruno Klein
Bruno Klein

Reputation: 3367

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?

Upvotes: 13

Views: 3719

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457362

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

Upvotes: 12

Related Questions