Skip Navigation LinksHome > Categories > Code from a Category

Using the BackgroundWorker Component in .NET 2 applications



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 89
Add Date: 12/31/2011
The BackgroundWorker Component allows a form to run an operation asynchronously. This is very useful when we deal with such kind of operations as database transactions, image downloads etc.
In this case our user interface can hang (or not appear until loading will be finished). In this article I will show (step-by-step) how you can use the BackgroundWorker Component in .NET 2 applications to execute time-consuming operations. The examples are written using C#.

As usually, we create a small test project, named "TestBGW" and having only one form ("FormBGW"):



We are going to use the BackgroundWorker Component for some database transaction operation (for example, getting some DataTable). First of all drag and drop onto our form the BackgroundWorker Component:



We will use the DataTable to set DataSource property of the DataGridView1. We also should update our user interface and should tell the user, that all our processes are "OK" and he/she should not worry. With this purpose we will use a StatusStrip and a Timer:



In order to inform the user that our process is running we will use the toolStripProgressBar1 :



To inform the user about status of the process and time, that process has been running, we will use the toolStripStatusLabel1 and the toolStripStatusLabelTime. Our form now looks like this:



In order to simulate database transactions (of course, you can connect to real database; this simulation is only for our test purpose) we use GetData.dll. With this purpose we add reference to GetData.dll and write the getDataTable method:



To start our asynchronous operation we use the RunWorkerAsync method:



The BackgroundWorker Component has three events:



The DoWork event occurs when the RunWokersAsync method is called . Here we "do" our time-consuming work (to do process "time-consuming" we load at least 100000 rows), let the user know that loading is in the progress and set our DataTable as Result of our asynchronous operation:



In our case (I mean the real situation, when we get some DataTable from some database) we cannot step "into" process and "trace" rows (row-by-row), and, of course, we do not know how many rows we can receive from the database according to our request. Because of that we will not use such method of the DoWorkEventArgs as ReportProgress, which raises the ProgressChanged event. We will use Timer1 to show the user, that our process runs (we will "fill out" the toolStripProgressBar1 up to the maximum and then we will begin from the minimum; that is: we will make some cycle). With this purpose we will add to the timer1_Tick the follow code:



To this method we also will add the code, that allows the user to know how much time the loading is continued. This will be something like that:



where ts is "now" time (see the full code below).
By the way, just to try how the ReportProgress works, you can add the follow code to the backgroundWorker1_DoWork method:



and then the following to the backgroundWorker1_ProgressChanged :


The RunWorkerCompleted event occurs when the background operation has completed. Here we will "close" all our messages about loading and bound the dataGridViewCities to the datatable with the help of the RunWorkerComletedEventArgs:



The full code of the FormBGW.cs is the following:


Now, if we start our project, we will see :

And after loading:


CONCLUSION

I hope that this article will help you to use the BackgroundWorker Component in your .NET 2 applications to execute time-consuming operations.

Good luck in programming !

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.