Events and Backgroundworkers

A computer can only perform one task. That’s a disadvantage when a task is time consuming. The systems seems to be freezing, doing it’s task or it can be in a loop without even knowing. So the OS developers setup a solution where processor time is shared. There is same administration to monitor and persist the processes running: a task of process A can be paused, the state will be stored, and a task of process B gets some resources. After a while the state of process B is stored, process A is restored and continues. This is multitasking and each process is a thread.
When you have a time consuming task, it not so user friendly to have an interface that freezes and windows show your app is not responding. To handle this more gently, you can use events or -one step further – create a new thread.


Image you have two classes, one is the worker, it performs a dedicated time consuming task, like scanning files or sending fiels over ftp.
The second class is the client, it’s using the first. The first only serves, but doesn’t know anything about the client class that is using him. But while doing it’s task, the rest must wait. During that time the client cannot even request a status update.
However, using events you can give the client some attention.

In the worker you can add the next code:

public event EventHandler CallBack;

When constructing the worker in the client class, you can connect a method of the client to this event.

WorkerClass workerClass = new WorkerClass();
workerClass.CallBack += MyClientMethod;


public void MyClientMethod(sender s, EventArgs e)
 // when method is invoked, the worker is still alive and running

In the Worker, you can add some code in the process

bool finished = false;
int step=0;
 // dosomething in this case wait 250 milliseconds
 notfinished = true;
 // after 4 times 250 ms of sleep, and if set the event is set by the client
 // invoke the event - so the client method can run.
 if (step % 4 == 0 && CallBack != null){
 CallBack(this, new EventArgs());

Underneath an event is using a delegate. You can also skip the event and use delegates, but using events and eventhandlers is easier.

The backgroundworker

In Windows Forms you can add a BackGroundWorker to a form or a user control.
A BackGroundWorker enables you to use another thread, without much overhead. The BackGroundWorker is like a box with another universe: you can put your work in this box where it will be executed in it’s own thread, so it will not cost you time, you can continue your live.
To keep in touch with the other universe the BackGroundWorker has 3 events you can connect to:

  • bgwWorker.DoWork – you can add a method that starts the work
    bgwWorker.RunWorkerCompleted – you can add a method that is executed when the work finished, so you can give feedback, cleanup things etc.
    bgwWorker.ProgressChanged – you can add a method that can give feedback about the progress of the process

When those event are set, you can start the work by invoking:

Meanwhile, you can put you UI elements in disabled state, but update a progressbar with every ProgressChanged event and enable and update the UI on the RunWorkerCompleted event.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s