11
votes

By default the thread pool in .NET works with background threads.

I can't change it to run as foreground.

How do I change this to run as foreground threads?

Edit:

I need to use this because I'm changing my App due high volume processing (before it I was manually handling threads, which are Foreground ones by default), and I don't want to change my Application just to adapt it to a background threading scenario..

I'm wondering why Thread pool is not Foreground by default

3
You want them to be foreground to prevent your process from terminating? You could keep a reference count as to how many active background threads you have an don't terminate your main (presumably foreground, UI) thread until the count reaches zero. - i_am_jorf
@jeffamaphone +1. Smells like "I do not know how to program that properly, so I take a hammer and somehow get it working, abusing everything I do not know". - TomTom
Well, I don't want to assume too much, but I do prefer questions which include why you want to do something, as frequently there is a better way that comes from experience. - i_am_jorf
What do you mean by "due high volume processing". If thread pool does not have the desired behavior then you should consider maybe it is not the right tool. - paparazzo
For the record, you can change the current ThreadPool thread to a foreground thread, but doing that is a terrible idea. - svick

3 Answers

19
votes

The notion of a "background thread" means very little in .NET. The Thread.IsBackground property is just a simple flag that helps the CLR decide whether or not it should rudely abort the thread when the AppDomain gets unloaded or whether it should wait for the thread to exit voluntarily.

You can just change the property if you want a thread-pool thread to keep running at program exit. This snippet shows how that's possible:

using System;
using System.Threading;

class Program {
    static void Main(string[] args) {
        var sync = new ManualResetEvent(false);
        ThreadPool.QueueUserWorkItem((_) => {
            Console.WriteLine("Running");
            Thread.CurrentThread.IsBackground = false;
            sync.Set();
            Thread.Sleep(5000);
            Console.WriteLine("Done, should exit now");
            Thread.Sleep(250);
        });
        sync.WaitOne();  // Ensures IsBackground is set
    }
}

Do keep your eyes on the ball here. If this is important to you then there's a high likelihood you are doing something wrong.

2
votes

You can set thread pool threads as foreground.

class Program
{
    static void Main(string[] args)
    {
        new Action(() => Worker()).BeginInvoke(null,null);

        Thread.Sleep(1000);
        Console.WriteLine("Main ends here");
    }

    static void Worker()
    {
        Thread.CurrentThread.IsBackground = false;

        Console.WriteLine("Worker started. Bg {0} Tp {1}.", 
            Thread.CurrentThread.IsBackground, 
            Thread.CurrentThread.IsThreadPoolThread);

        Thread.Sleep(5000);
        Console.WriteLine("Worker ends here");
0
votes

I do not believe you can, you will have to come up with a custom ThreadPool in order to do this.

The threads in the managed thread pool are background threads. That is, their IsBackground properties are true. This means that a ThreadPool thread will not keep an application running after all foreground threads have exited.

When Not to Use Thread Pool Threads

There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:

  • You require a foreground thread.

From this link And This One