Recent Posts

Tags

News

  • Sentient

    My services as a speaker, consultant, trainer and developer are available through Sentient.

    Sentient is a leading provider of software development, consultancy and training services on the Microsoft platform.

    Please feel free to contact me if you have a question about something posted in my blog.

Community

Email Notifications

Links

Archives

Sentient thoughts about .NET

Jonathan Greensted's .NET weblog

Threading - IsBackground

During my System.Threading talk last night one of the questions from the audience asked what the IsBackground property on the Thread classes was used for. 

The intellisense for this property says "Gets or Sets a value indicating whether or not the tread is a background thread." which to be honest I think we could have guessed!  What this description lacks is any information about why you would want to set it.

It's always embarrassing when you can't immediately answer a question from the audience but in this case I was stumped so I promised to investigate the answer and here it is.

The IsBackground property allows you to tell the runtime if the thread is a foreground (UI) thread or a background thread.   By default all threads are created as foreground threads unless you change this property.

So why do we care?

Well, the .NET runtime does something special when the last foreground thread terminates - it aborts all the background threads and terminates the application.  (We know Thread.Abort is bad however if the application is being shutdown anyway that badness has a very limited duration.)

This can be nicely illustrated by creating a WinForms application which launches a background thread which runs forever.   By default when you run your application it will run forever unless you explicitly abort the background thread or the application.  This can be seen by running your simple application, closing the Form1 window and noticing the application is still running.

If however you set IsBackground to true after creating your background thread:

Thread t = new Thread(new ThreadStart(BackgroundThread));

t.IsBackground = true;

t.Start();

You will notice that when you close the only window (Form1) the application will terminate.

I will certainly be updating our System.Threading presentation to explain this behaviour more clearly in the future.  A great question and something we should all be doing.

Comments

jonathangreensted said:

...and fyi...

In NETCF 1.0 all threads are IsBackground=false.
You must design your own measures for terminating your threads and that is without Abort (since that is not supported either!. CF 2.0 rectifies both of these :-)
# February 20, 2005 1:50 PM

jonathangreensted said:

Thanks for that Daniel.

Threading in the NETCF world is certainly more raw/interesting.

Jonathan
# February 20, 2005 1:51 PM

TrackBack said:

^_~,pretty good!csharpsseeoo
# May 17, 2005 9:34 PM

TrackBack said:

Threading - IsBackgroundooeess
# July 17, 2005 2:18 AM

TrackBack said:

# July 27, 2005 6:09 AM

TrackBack said:

Threading - IsBackgroundooeess
# July 31, 2005 10:26 PM

TrackBack said:

A good post on the IsBackground property of a Thread by Jonathan Greensted.
IsBackground
# October 18, 2005 5:53 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)