VIBlend
Home Products Downloads Purchase Support Forum About Blog

Implementing multi-threading in WinForms using the BackgroundWorker class and the VIBlend WinForms ProgressBar.

by viblend 31. January 2012 04:24
In this post we’ll create a simple multi-threaded WinForms application and will use the VIBlend WinForms ProgressBar control to report the progress to the user. The application will loop through numbers from 0 to 999. The progress bar will be filled during the process.

Let’s start by creating a Windows Application. In designer of the form, add the BackgroundWorker component from the “Components” category. The component is added to the bottom section of the designer.  Click it and set the WorkerReportsProgress property to true - this allows us to send progress notifications. Subscribe to the BackgroundWorker's DoWork and ProgressChanged events. The background task is handled in the DoWork event. The handler for this event will be called from a new worker thread. The ProgressBar will be updated in the ProgressChanged event handler. In order to notify about the progress you should call the ReportProgress method from the DoWork event handler. Please note that the ReportProgress method will work only when the WorkerReportsProgress property of the BackgroundWorker class is set to true.  

The next step is to create the User Interface. Drag and drop vButton and vProgressBar controls from the Visual Studio toolbox. Subscribe to the Button's Click event. To start doing the work we will call the RunWorkerAsync method of the BackgroundWorker in the Button's Click event handler.

In the ProgressChanged event handler, we will update the ProgressBar's value.

C#

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   // update the progress bar
    this.vProgressBar1.Value = e.ProgressPercentage;
    this.vProgressBar1.Refresh();
}

VB .NET

Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
   ' update the progress bar
Me.vProgressBar1.Value = e.ProgressPercentage
Me.vProgressBar1.Refresh()
End Sub

Report the Progress in the DoWork event handler.

C#

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
              int limit = 999;
for (int currNumber = 0; currNumber < 999; currNumber++)
{
       int progressPercentage = (100 * currNumber) / (limit);
                   worker.ReportProgress(progressPercentage);
}
}

VB .NET

Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
 Dim limit As Integer = 999
For currNumber As Integer = 0 To 998
  Dim progressPercentage As Integer = (100 * currNumber) / (limit)
  worker.ReportProgress(progressPercentage)
Next currNumber
End Sub
 
 
Comments are closed

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET