user496949
user496949

Reputation: 86185

How should I create a background thread?

The thread created by the following is the foreground thread

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

Can I make the thread created background?

Upvotes: 11

Views: 24118

Answers (5)

mareon
mareon

Reputation: 171

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.

Upvotes: 16

Berryl
Berryl

Reputation: 12863

try this code:-

    Thread.IsBackground = true

Upvotes: 1

Ani
Ani

Reputation: 113472

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 };

Upvotes: 19

bbosak
bbosak

Reputation: 5529

new Thread(new ParameterizedThreadStart(DoJob)) { IsBackground = true };

Should be IsBackground, not IsBackGround

Upvotes: 1

Saif Khan
Saif Khan

Reputation: 18812

Try

workingThread.IsBackGround = true;

Upvotes: 3

Related Questions