Tag Archives: DoWork

BackgroundWorker Part 2 – Reporting Class Instance Status (C#)

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++)
		{
			if (backgroundworker1.CancellationPending)
			{
				e.Cancel = true;
			}
			else
			{
				backgroundworker1.ReportProgress(Convert.ToInt32(i*100/numericUpDown1.Value));
				System.Threading.Thread.Sleep(100);
			}
		}
	}

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.
Continue reading

Using Asp.Net BackgroundWorker To Create Progress Bar

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.

Continue reading

Fill DataGridView and cancel process using DataAdapter and BackgroundWorker

Introduction

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.

Background

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

BackgroundWorker Intro Part 1 – Work with ProgressBar

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, ProgressChanged, RunWorkerCompleted.
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