Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Tip/Trick

BackgroundWorker in .NET Console Application

Rate me:
Please Sign up or sign in to vote.
2.33/5 (4 votes)
21 May 2012CPOL1 min read 28.9K   2   4
Behavior of BackgroundWorker thread for Console and Windows form apps.

Introduction

Behavior of BackgroundWorker thread for Console and Windows Forms apps.

Background

Today I was just doing net surf and came across an interesting question: 'Can the progress event of BackgroundWorker execute after completed event'. At first I thought no, but when I tried this with a Console application, I was also able to reproduce this issue. Now the question is, how come this scenario occurs in a Console app and not in Windows Forms. Pretty interesting, right? Now coming to Windows Forms, this issue will never occur due to message queuing support. The Windows message queue takes very good care of execution sequence of the events. This clearly mean that the progress event may run after the DoWork has completed, but the completion event will always happen afterwards. Another interesting thing here is the SynchronizationContext, which helps in maintaining all these sequencing. But when talking about a Console application, none of the above holds true. There is no SynchronizationContext installed and the events just end up in getting run in the threadpool thread, which doesn't guarantee any order.

Using the code

I created a console app and used Backgroundworker with all the required event handlers. In the progress event handler, I added the below lines:

C#
Console.WriteLine("One");  
Console.WriteLine("Two");
Console.WriteLine("Three");
Console.WriteLine("Four");

Points of Interest

On executing the console application, I found that output messages are not in order, which I mentioned in code. On the standard output, I received Two, Three, Four, One and sometimes I received One, Two, Three, Four and sometimes, I also found one of the messages missing and in the output, I got only Two, Three, Four. But in Windows Forms, I always get the output in the correct order as One, Two, Three, Four. 

This article was originally posted at http://shwetamannjain.blogspot.in

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 2 Pin
Santhosh Kumar Jayaraman15-Aug-12 6:29
Santhosh Kumar Jayaraman15-Aug-12 6:29 
No code. Will be good if Sample code or demo is attached
QuestionCode? Pin
a6rg5x21-May-12 3:11
a6rg5x21-May-12 3:11 
General[My vote of 1] Where is the code? Pin
Andreas Gieriet20-May-12 21:54
professionalAndreas Gieriet20-May-12 21:54 
GeneralRe: [My vote of 1] Where is the code? Pin
Shweta Lodha21-May-12 3:24
Shweta Lodha21-May-12 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.