12

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?

Bruno Klein
  • 3,217
  • 5
  • 29
  • 39

1 Answers1

12

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

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810