34

How do I set a thread to a daemon thread in C#?

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
Epaga
  • 38,231
  • 58
  • 157
  • 245
  • 6
    http://meta.stackexchange.com/questions/17463/should-i-ask-a-question-i-know-the-answer-to – Epaga Feb 17 '11 at 14:30

2 Answers2

54

Though you have already answered your own question, I would still like to elaborate more on it.

In C# .NET, unlike in Java

   C# Background threads ~ Java Daemon threads  
   C# Foreground threads ~ Java User threads

By default, threads you create explicitly are foreground threads.

"Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating." (reference)

You can make a thread Daemon by

thread.IsBackground = true;  
Paul Du Bois
  • 2,097
  • 1
  • 20
  • 31
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • excellent, thanks! :) and re: answering my own question, see http://meta.stackexchange.com/questions/17463/should-i-ask-a-question-i-know-the-answer-to – Epaga Feb 19 '11 at 17:15
11

Like this:

myThread.IsBackground = true; 
jjnguy
  • 136,852
  • 53
  • 295
  • 323
Epaga
  • 38,231
  • 58
  • 157
  • 245