There are times when you need to do some heavy work. Of course there are times in everyone'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 into the picture. As the name itself suggests, Background Worker means ... background worker. It is actually a separate thread with a completed event. There are two basic events in Background Worker that actually do the work, yeah I mean literally,
- DoWork
- RunWorkerCompleted
Are the two. DoWorker does the heavy lifting. RunWorkerCompleted is called when DoWorker finishes doing the heavy lifting. Let me first give you my mainpage.cs class that has the Background Worker initialized and its events created.
1: public partial class MainPage : UserControl
2: {
3: readonly BackgroundWorker _bWorker = new BackgroundWorker();
4:
5: public MainPage()
6: {
7: InitializeComponent();
8: _bWorker.DoWork += _bWorker_DoWork;
9: _bWorker.RunWorkerCompleted += _bWorker_RunWorkerCompleted;
10: }
11:
12: void _bWorker_DoWork(object sender, DoWorkEventArgs e)
13: {
14: //Heavy lifting goes here
15: }
16:
17: void _bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
18: {
19: //When heavy lifting completed
20: }

Gowtham RajamanickamPosted May 20, 2015, 1:02 AM
good
Kundan KumarPosted Jun 13, 2013, 4:05 AM
Very nice article.