Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Threads in C#

I am new to C#.I learnt that normally all threads are foreground until unless you explicitly specify it as "background" thread using IsBackGround= true .

Some doubts popped in to my mind.

1) What is the advantage of keeping a thread as background thread?

2) When executing the folowing code :

    static void Main(string[] args)
    {
        Thread worker = new Thread(SayHello);
        worker.IsBackground = true;
        worker.Start();
        Console.WriteLine("Hello From Main");
    }

    static void SayHello()
    {
        Console.WriteLine("Hello World");
        Console.ReadKey(true);
    } 

i need to use worker.Join() to keep the main thread to wait as the program terminates immediately. Apart from Join() could i use other techniques to keep the main thread wait ?

like image 935
user186973 Avatar asked Oct 11 '09 09:10

user186973


People also ask

What are background threads?

Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.

Which thread is executed in background?

In this example, the callback is executed in the calling thread, which is a background thread. This means that you cannot modify or communicate directly with the UI layer until you switch back to the main thread.

What is the use of background thread in C#?

Background Threads The property used for background thread is IsBackground that gets or sets a value indicating whether a thread is a background thread. The default value of this property would be false because the default threads created are Foreground Threads.

What is a foreground thread?

A thread which keeps on running to complete its work even if the Main thread leaves its process, this type of thread is known as foreground thread. Foreground thread does not care whether the main thread is alive or not, it completes only when it finishes its assigned work.


2 Answers

1) What is the advantage of keeping a thread as background thread?

The Advantage is that a Background Thread doesn't stop the Program from terminating. In a large Application it could be a bit hard to deactivate all threads if you want to quit the Application.

Apart from Join() could i use other techniques to keep the main thread wait?

If yo want to make the Main program wait why do you make the thread a background thread in the first place then??? Besides of Join() you could use a EventWaitHandle or Monitor to make the main method wait.

like image 97
codymanix Avatar answered Sep 18 '22 13:09

codymanix


All it means is whether this thread will keep the process alive. If all the threads in your process are marked background then .Net will shut down your process and force it to exit.

In answer to your question, yes you have to join as the thread which is in the background will not keep it alive, thus when the startup thread leaves Main() then it will allow the application to exit.

like image 35
Spence Avatar answered Sep 19 '22 13:09

Spence