# .NET and More > WPF, WCF, WF >  Backgroundworker in WPF?

## Zolomon

Hello!

How do I create and use a backgroundworker in WPF, C#?

Been googlin' around for ages without any luck.
//Zolomon

----------


## chris128

Im guessing you are like me and are used to just dragging and dropping one in from the toolbox? I was a bit stumped when I first started to use WPF but dont worry its pretty easy to do still (and this is apparently the way that a lot of people do it in winforms anyway).

All you need to do is declare it yourself at class level and if you use the WithEvents keyword then you dont need to hook up the event handlers yourself or anything.

So for example, at class level (ie not in a sub or function) :

vb.net Code:
Private WithEvents bgworker As New ComponentModel.BackgroundWorker

Then you can just use the drop down list at the top of the code editor to select "bgworker" (as I have named it, obviously you can give it a different name if necessary) and then select the DoWork event or whichever event you want to create a handler for from the drop down list next to it.
Alternatively you can type out the event handlers manually, so for example:

vb.net Code:
Private Sub bgworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgworker.DoWork
 End Sub

That help?  :Smilie:

----------


## Zolomon

A little, but I don't think the drop-down thingie works for C#.  :Smilie:

----------


## chris128

Ah sorry I havent tried it in C# but I assumed it would be the same... one sec and I'll give it a go

----------


## chris128

OK bear in mind that I have never touched C# before (seen plenty of examples though thanks to MS not providing VB documentation for many things...) so this might be rubbish but I think it should go something like this:

At the top of your code:

c#.net Code:
using System.ComponentModel
At class level:

c#.net Code:
BackgroundWorker bgworker = new BackgroundWorker();
In your window_load event or wherever suits, hook up the event handlers:

c#.net Code:
bgworker.DoWork += new DoWorkEventHandler(bgworker_DoWork);
bgworker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(bgworker_RunWorkerCompleted);
Define the event handlers:

c#.net Code:
private void bgworker_DoWork(object sender,DoWorkEventArgs e)
        {
           //Do background worker stuff here
        }

c#.net Code:
private void bgworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //Background worker has finished
         }

Any use?

----------


## Zolomon

Thanks! Working like a charm!  :Big Grin:

----------


## chris128

no worries, dont forget to mark the thread as resolved  :Smilie:

----------


## jmcilhinney

The C# IDE will help you create event handlers too, although in quite a different way to VB.  You don't have to write out the event handler yourself.  You just use the Tab key at the opportune moments to have the IDE generate the code for you.  You may already have been aware but in case you weren't, and for others, you just type the "+=" and then hit Tab twice to insert the skeleton code.

----------

