--- title: "How to: Download a File in the Background" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "BackgroundWorker component" - "background tasks" - "Asynchronous Pattern" - "forms [Windows Forms], multithreading" - "components [Windows Forms], asynchronous" - "forms [Windows Forms], background operations" - "threading [Windows Forms], background operations" - "background operations" ms.assetid: 9b7bc5ae-051c-4904-9720-18f6667388bd --- # How to: Download a File in the Background Downloading a file is a common task, and it is often useful to run this potentially time-consuming operation on a separate thread. Use the component to accomplish this task with very little code. ## Example The following code example demonstrates how to use a component to load an XML file from a URL. When the user clicks the **Download** button, the event handler calls the method of a component to start the download operation. The button is disabled for the duration of the download, and then enabled when the download is complete. A displays the contents of the file. [!code-csharp[System.ComponentModel.BackgroundWorker.IsBusy#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/CS/Form1.cs#1)] [!code-vb[System.ComponentModel.BackgroundWorker.IsBusy#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/VB/Form1.vb#1)] **Downloading the file** The file is downloaded on the component's worker thread, which runs the event handler. This thread starts when your code calls the method. [!code-csharp[System.ComponentModel.BackgroundWorker.IsBusy#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/CS/Form1.cs#3)] [!code-vb[System.ComponentModel.BackgroundWorker.IsBusy#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/VB/Form1.vb#3)] **Waiting for a BackgroundWorker to finish** The `downloadButton_Click` event handler demonstrates how to wait for a component to finish its asynchronous task. If you only want the application to respond to events and do not want to do any work in the main thread while you wait for the background thread to complete, just exit the handler. If you want to continue doing work in the main thread, use the property to determine whether the thread is still running. In the example, a progress bar is updated while the download is processing. Be sure to call the method to keep the UI responsive. [!code-csharp[System.ComponentModel.BackgroundWorker.IsBusy#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/CS/Form1.cs#2)] [!code-vb[System.ComponentModel.BackgroundWorker.IsBusy#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/VB/Form1.vb#2)] **Displaying the result** The `backgroundWorker1_RunWorkerCompleted` method handles the event and is called when the background operation is completed. This method first checks the property. If is `null`, then this method displays the contents of the file. It then enables the download button, which was disabled when the download began, and it resets the progress bar. [!code-csharp[System.ComponentModel.BackgroundWorker.IsBusy#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/CS/Form1.cs#4)] [!code-vb[System.ComponentModel.BackgroundWorker.IsBusy#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.ComponentModel.BackgroundWorker.IsBusy/VB/Form1.vb#4)] ## Compiling the Code This example requires: - References to the System.Drawing, System.Windows.Forms, and System.Xml assemblies. ## Robust Programming Always check the property in your event handler before attempting to access the property or any other object that may have been affected by the event handler. ## See also - - [How to: Run an Operation in the Background](how-to-run-an-operation-in-the-background.md) - [How to: Implement a Form That Uses a Background Operation](how-to-implement-a-form-that-uses-a-background-operation.md)