About long time ago, I already wrote about basic of BackgroundWorker component, BackgroundWorker Part 1.
It contained simple example on how to use BackgroundWorker and also ProgressBar. If you want to look then go to this url
This part 2 will go for more advance implementation example. This means application itself is simple but its coding technique is more advance.
As you may know by now that BackgroundWorker makes procedure run asynchronously. For example I have a procedure to run and the progress change of running procedure is reported.
This is possible because of this procedure run asynchronously by BackgroundWorker.
In order to run a BackgroundWorker, we have to use DoWork event handler function. So we need to write a procedure we want inside that DoWork function.
Most of examples show how to just write a whole codes in DoWork. For example below code:
void backgroundworker1_DoWork(object sender, DoWorkEventArgs e)
for (int i = 1; i <= numericUpDown1.Value; i++)
e.Cancel = true;
So it shows simple looping and send its report progress but the whole iteration coding is inside DoWork. When more complex requirement is needed then the coding become cumbersome.
We need a way to separate main procedure from DoWork function. So, it will also make loose coupling.
This procedure inside DoWork must have asynchronous capability. To accomplish this, I use Thread object. Thread will make asynchronous process.
BackgroundWorker is a component that runs asynchronously as in the background. We need to create and use it along with Ajax to occupy async process.
In the previous post, I already have an example of create a BackgroundWorker example using client script, ASP .Net Ajax BackgroundWorker Example Using Client Script & PageMethods.
In this post, I use System.ComponentModel.BackgroundWorker class to run a background process. Its progress status will be showed in progress bar.
That progress bar is located inside ModalPopupExtender Ajax Control Toolkit. So when backgroundWorker is running no UI activity allowed exept that inside the ModalPopupExtender.
This example only do simple calculation. After calculation is completed the result shows on result div element. Its progress percentage showed as a progress bar.
ADO.NET DataAdapter is a bridge between data source and DataSet. The implementation of DataAdapter can be SQLDataAdapter, OLEDBDataAdapter, iDB2DataAdapter etc. DataAdapter.fill method fills data source recordset to a DataSet or DataTable returned by a ‘select’ query.
After populating to a DataSet and DataTable, database connection can be closed but DataTable data still able to access. This is known as Disconnected ADO.Net operation.
We do not have to re-connect to database if we want to retrieve same data with previously DataAdapter.Fill work. We can access the DataTable’s data like an array.
Because of that performance of disconnected operation is better compared with connected one in some cases.
If the ‘select’ query has result of thousands or millions record DataAdapter.Fill method will populate them all thus long run might occur, DataAdapter.Fill looks like slow.
However there is no default method to cancel DataAdapter.Fill. So to terminate long Fill process we have to kill it manually and the application will close.
We need to know how to cancel DataAdapter.Fill on the fly for particular reason.
Posted in Visual Basic .Net
Tagged Abort DataAdapter.Fill, BackgroundWorker, Cancel DataAdapter.Fill, DataAdapter.Fill, DataTable RowChanged, Disconnected operation, SQLCommand, SQLConnection, SqlDataAdapter, VB .Net, Visual Basic
At A Glance
BackgroundWorker commonly use on Winform application. Basically it is an asynchronous process. Its thread is separate from main.
On user point of view backgroundworker works on background. Developers codes Winform async process easily with BackgroundWorker.
Here’s a Winform brief code of BackgroundWorker:
BackgroundWorker bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.WorkerSupportsCancellation = true;
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
void bgw_DoWork(object sender, DoWorkEventArgs e)
However on Asp .Net Web Application there is no default BackgroundWorker server control. We have to use Asp .net Ajax to make a web async process.
Filling data to DataGridView with SqlDataAdapter is a common task but to cancel it in the middle of fill process need a tricky way. Especially when SqlDataAdapter returning large DataSet then no progress status informed to user since SqlDataAdapter need time to fetch DataSet. Also this project does not need to have button like “More records” to fetch more dataset. We can retrieve as much records you like and cancel it any time.
We need to show records immediately and user can cancel the retrieval process whenever they like. The basic idea is using int recordcount = SqlDataAdapter.Fill(int startrow, int maxrecords, DataTable dt) method at BackgroundWorker.DoWork event. That Fill method is similar to paging in web page. BackgroundWorker execute SqlDataAdapter.Fill in loop until it has no more record to fetch and we can cancel the process before or after SqlDataAdapter.Fill. Continue reading
Posted in C#
Tagged BackgroundWorker, C#, C# Example, DataGridView, dataset, DoWork, ProgressChanged, RunWorkerCompleted, SqlDataAdapter, Windows Form, WinForm
Winform application usually need asynchronous process. Progress status of this process commonly showed with progressbar control.
As a default Thread class accomplish the asynchronous process but .Net has built in BackgroundWorker component to do async easily.
BackgroundWorker component/class has events which are
DoWork handles main async job. ProgressChanged handles displaying async progress info to user. RunWorkerCompleted handles event when async process is completed.
This BackgroundWorker has Cancel ability, so that user can cancel asnyc in the middle of its process. Continue reading