Click here to Skip to main content
13,503,224 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 19 May 2012

BackgroundWorker in .NET Console Application

, 21 May 2012
Rate this:
Please Sign up or sign in to vote.
Behavior of BackgroundWorker thread for Console and Windows form apps.


Behavior of BackgroundWorker thread for Console and Windows Forms apps.


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:


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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
Santhosh Kumar J15-Aug-12 6:29
memberSanthosh Kumar J15-Aug-12 6:29 
QuestionCode? Pin
millerb21-May-12 3:11
membermillerb21-May-12 3:11 
General[My vote of 1] Where is the code? Pin
Andreas Gieriet20-May-12 21:54
memberAndreas Gieriet20-May-12 21:54 
GeneralRe: [My vote of 1] Where is the code? Pin
Shweta Lenka21-May-12 3:24
memberShweta Lenka21-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 21 May 2012
Article Copyright 2012 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid