Replace this file name with a valid file Sub backgroundWorker1_DoWork( _
This thread starts when your code calls the RunWorkerAsync method. The file is downloaded on the BackgroundWorker component's worker thread, which runs the DoWork event handler. ' The main entry point for the application. Me.downloadButton.Name = "downloadButton" Me.downloadButton.Location = New (12, 12) ' the contents of this method with the code editor. ' Required method for Designer support - do not modify If disposing AndAlso (components IsNot Nothing) Then Protected Overrides Sub Dispose(ByVal disposing As Boolean) ' true if managed resources should be disposed otherwise, false. #Region "Windows Form Designer generated code"
' Enable the download button and reset the progress bar. MessageBox.Show("Failed to download file", "Download failed", MessageBoxButtons.OK, MessageBoxIcon.Error) MessageBox.Show(document.InnerXml, "Download Complete") ' Set progress bar to 100% in case it isn't already there. Handles backgroundWorker1.RunWorkerCompleted Private Sub backgroundWorker1_RunWorkerCompleted( _īyVal e As RunWorkerCompletedEventArgs) _ ' Replace this file name with a valid file name. ' responsive during the asynchronous operation. ' Keep UI messages moving, so the form remains ' while checking IsBusy to see if the background task is
' such as update a progress bar, you can do so in a loop ' If you want to do something else in the main thread, ' wait until the RunWorkerCompleted event is raised. ' can exit the handler and the application will ' Once you have started the background thread you ' Disable the button for the duration of the download. ' Start the download operation in the background. Private document As XmlDocument = Nothing Private WithEvents backgroundWorker1 As BackgroundWorker Private WithEvents progressBar1 As ProgressBar Private WithEvents downloadButton As Button / The main entry point for the application. This.AutoScaleDimensions = new (6F, 13F) += new System.EventHandler(this.downloadButton_Click) Protected override void Dispose(bool disposing) / true if managed resources should be disposed otherwise, false.
#region Windows Form Designer generated code Enable the download button and reset the progress bar.
Set progress bar to 100% in case it's not already there. Replace this file name with a valid file void backgroundWorker1_RunWorkerCompleted( responsive during the asynchronous operation. Keep UI messages moving, so the form remains while checking IsBusy to see if the background task is such as update a progress bar, you can do so in a loop Or if you want to do something else in the main thread, wait until the RunWorkerCompleted event is raised. can exit the handler and the application will Once you have started the background thread you Disable the button for the duration of the download. Start the download operation in the background. Private void downloadButton_Click(object sender, EventArgs e) Instantiate BackgroundWorker and attach handlers to itsīackgroundWorker1 = new () īackgroundWorker1.DoWork += new (this.backgroundWorker1_DoWork) īackgroundWorker1.RunWorkerCompleted += new (this.backgroundWorker1_RunWorkerCompleted) Private BackgroundWorker backgroundWorker1
A MessageBox displays the contents of the file. The button is disabled for the duration of the download, and then enabled when the download is complete. When the user clicks the Download button, the Click event handler calls the RunWorkerAsync method of a BackgroundWorker component to start the download operation.
The following code example demonstrates how to use a BackgroundWorker component to load an XML file from a URL. Use the BackgroundWorker component to accomplish this task with very little code. Downloading a file is a common task, and it is often useful to run this potentially time-consuming operation on a separate thread.