There are times when you need to do some heavy work. Of course there are times in every one's life when mom forces you to clean your room, but I am not talking about that “heavy” work, not now I mean. From heavy work I mean like when you need to iterate in a very large collection to make changes in its entities or something like that. If you are planning to do it in the main thread of Silverlight itself, then believe me buddy, you are in serious trouble. Doing that kind of stuff might block your user interface.

So here comes background worker in the picture. As the name itself suggests, background worker means…background worker. It is actually a thread with completed event. There are two basic events in Background worker that actually do work, yeah I mean literally,

  • DoWork
  • RunWorkerCompleted

are the two. DoWorker do the heavy lifting. RunWorkerCompleted is called when DoWorker completed doing heavy lifting. Let me first give you my mainpage.cs class which have Background worker initialized and its events created.


Code
   1: public partial class MainPage : UserControl
Code
   2: {
Code
   3:     readonly BackgroundWorker _bWorker = new BackgroundWorker();
Code
   4:
Code
   5:     public MainPage()
Code
   6:     {
Code
   7:         InitializeComponent();
Code
   8:         _bWorker.DoWork += _bWorker_DoWork;
Code
   9:         _bWorker.RunWorkerCompleted += _bWorker_RunWorkerCompleted;
Code
  10:     }
Code
  11:
Code
  12:     void _bWorker_DoWork(object sender, DoWorkEventArgs e)
Code
  13:     {
Code
  14:         //Heavy lifting goes here
Code
  15:     }
Code
  16:
Code
  17:     void _bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
Code
  18:     {
Code
  19:         //When heavy lifting completed
Code
  20:     }
Code
  21: }

I have defined here, a read-only object of background worker _bWorker. The two events DoWork and RunWorkerCompleted, that we were talking about, are created in the constructor. I put my heavy code in the DoWork event, and code that is depended on the calculations returned from the heavy code will go into RunWorkerCompleted event.
Background worker will not start doing work, not until you tell it to do so. And you can tell it by calling RunWorkerAsync() function.

In Xaml of my main page I have added a button, Do Work. Here is my page's xaml.

Code
   1: <UserControl x:Class="Background.MainPage"
Code
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Code
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Code
   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Code
   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Code
   6:     mc:Ignorable="d"
Code
   7:     d:DesignHeight="300" d:DesignWidth="400">
Code
   8:
Code
   9:     <Grid x:Name="LayoutRoot" Background="White">
Code
  10:         <Button Content="Do Work" Width="150"
Code
  11:                 Height="100" Click="ButtonBase_OnClick"/>
Code
  12:     </Grid>
Code
  13: </UserControl>

And on the click even of the button:

Code
   1: private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
Code
   2: {
Code
   3:    _bWorker.RunWorkerAsync();
Code
   4: }

RunWorkerAsync function orders background worker to start doing task assigned, yes in the background, of course.