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.
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
// update the progress bar
this.vProgressBar1.Value = e.ProgressPercentage;
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
' update the progress bar
Me.vProgressBar1.Value = e.ProgressPercentage
Report the Progress in the DoWork event handler.
private void worker_DoWork(object sender, DoWorkEventArgs e)
int limit = 999;
for (int currNumber = 0; currNumber < 999; currNumber++)
int progressPercentage = (100 * currNumber) / (limit);
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)