Tag Archives: Asynchronous

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

ASP .Net Ajax BackgroundWorker Example Using Client Script & PageMethods

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);
bgw.RunWorkerAsync();

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