2

I'm developing an application that, listen a queue from different thread but I have a problem about thread management. I started a background thread from my main application. It is work fine but after Main application finished, then child threads kill. Is there anyway to continue child thread after main application finished.

I start threads like below.

Thread myNewThread = new Thread(() => Executer.ProcessQueueMessages());
myNewThread.IsBackground = true;
myNewThread.Start();
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
rafet C.
  • 35
  • 1
  • 4
  • 2
    Nope there is no such way. You can make thread non-background and it will continue working after main thread finished - but then your main application process will not be really finished. – Evk Feb 26 '18 at 06:38
  • 1
    All the child threads will killed as soon as the main thread is killed. You have to wait before all the child threads are finished before killing main thread. – Manprit Singh Sahota Feb 26 '18 at 06:39

3 Answers3

5

As others have said in the comments, but decided not to provide a full answer, a background thread does not continue to run after the parent thread is terminated.

If you want this thread to keep running then you need to set it as a foreground thread.

This source: http://www.c-sharpcorner.com/UploadFile/ff0d0f/working-of-thread-and-foreground-background-thread-in-C-Sharp730/

explains nicely the difference and gives examples. An extract taken from this states;

In C# there're the following 2 kinds of threads.

  1. Foreground Thread
  2. Background Thread

Foreground Thread

Foreground threads are those threads that keep running even after the application exits or quits. It has the ability to prevent the current application from terminating. The CLR will not shut down the application until all Foreground Threads have stopped.

Background Thread

Background Threads are those threads that will quit if our main application quits. In short, if our main application quits, the background thread will also quit. Background threads are views by the CLR and if all foreground threads have terminated, any and all background threads are automatically stopped when the application quits. By default every thread we create is a Foreground Thread.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
1

When the process exits, the O/S cleans up any open handles, running threads, and any other locked resources. If it didn't do this, it would be very difficult to right the system if a process misbehaved. So what you're asking isn't possible.

If you have a long running job to execute and you want to make sure it is finished before exiting the program, the most common approach is to return a task and await it before exiting, e.g. to start the task do this:

var task = Task.Run( () => DoSomethingThatTakesALongTime() );

And to ensure it finishes do this:

await task;

Or this:

task.GetAwaiter().GetResult();
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

you can Use Join() to make current thread wait till child thread (myNewThread) finishes.

Thread myNewThread = new Thread(() => Executer.ProcessQueueMessages());
myNewThread.IsBackground = true;
myNewThread.Start();
myNewThread.join()
sun2
  • 1,118
  • 1
  • 13
  • 17