Click here to Skip to main content
Click here to Skip to main content

Tagged as

BackgroundWorker in .NET Console Application

, 21 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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. 

License

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

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 2 PinmemberSanthosh Kumar J15-Aug-12 7:29 
QuestionCode? Pinmembermillerb21-May-12 4:11 
General[My vote of 1] Where is the code? PinmemberAndreas Gieriet20-May-12 22:54 
GeneralRe: [My vote of 1] Where is the code? PinmemberShweta Lenka21-May-12 4:24 

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

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

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