Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I create a background thread?

Tags:

c#

.net

The thread created by the following is the foreground thread

  Thread workingThread = new Thread(new ParameterizedThreadStart(DoJob));

Can I make the thread created background?

like image 711
user496949 Avatar asked Nov 12 '10 02:11

user496949


2 Answers

Yes, you can; System.Threading.Thread has an IsBackground property.

Gets or sets a value indicating whether or not a thread is a background thread.

Thread workingThread = new Thread(new ParameterizedThreadStart(DoJob))
                                 { IsBackground = true };
like image 63
Ani Avatar answered Oct 16 '22 22:10

Ani


I know this is an older thread, but the most practical solution how create new Thread is this:

 new Thread(() => NameOfYourMethod()) { IsBackground = true }.Start();

If you need to create paramerized Thread, just do simple modification:

 new Thread(() => NameOfYourMethod(param1, param2...)) { IsBackground = true }.Start();

And that's all, I hope it helps someone :)

*Use this only if you don't need to store Treads for some reason.

like image 24
mareon Avatar answered Oct 16 '22 22:10

mareon