C# Thread Creation Question
When creating a new System.Thread instance the default value of IsBackground
is false. Can anybody shed the light on why this would be the default value would be not be true. My incl开发者_如何转开发ination, correct or not, when creating a thread is that it would be run in the background of the main thread.
Thank you for the quick responses.
I do provide functionality in the destructor of my component to allow for the thread to end gracefullly. I am using a ManualResetEvent
and Join
.
I fully understand that allowing the thread to end gracefully is correct and proper. What I am not conceptualizing is that why. by default, a thread must hang an entire application if it does not end successfully when the application is exiting.
IsBackground
means that the thread is terminated when the application terminates. This is rarely desirable behaviour, because it means that thread can't stop and clean up properly.
Instead, the app should signal the thread to terminate, wait until it has done so, and then close properly.
That's my summary of this blurb from the Thread.IsBackground
MSDN article:
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
This information from the MSDN page on IsBackground
explains the difference between background and foreground threads.
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
Typically, in .NET, if you are going to create your own thread, it will be for a long running process. In this situation, you would normally not want the thread to be destroyed if the user closes your main form. Instead, you'd want to have a good chance to handle cleanup properly.
If you just want to run a quick work item in a background thread, use the ThreadPool. This will typically be much better for anything that isn't a long running work item.
The concept of foreground versus background may be a bit confusing. As others have pointed out the background aspect only affects whether the runtime will shutdown the process or not. If no foreground threads remain, it will shutdown the process regardless of the number of running background threads once the last foreground thread terminates.
As for new threads running "in the background of the main thread" that isn't really the case. Each new thread will default to the same priority as you main thread, so they will be scheduled just as frequently as the main thread and thus the main thread doesn't really receive any special treatment in that sense. Threads are scheduled based on priority and not whether they happen to be foreground or background threads.
You can of course set both the background/foreground and priority of new threads, but the default is that new threads are equal to the initial thread.
> I fully understand that allowing the thread to end gracefully is correct and proper, but what I am missing is that why by default a thread must hang an entire application if it does not end successfully. <<
Unless those background threads are doing something very harmless, it may be better to hang the process (for a little while -- until they exit) than to force them to shut down -- if you make them background threads, you have no control over what they are doing when they exit. An alternative would be to keep track of the tasks that you have running in "background" threads. When your main thread wants to shut down, it should signal the threads to cancel, and give them a chance to exit gracefully. If they fail to exit gracefully, you could abort them (or leave them set as background, but don't let your main thread exit until you are ready to end the background threads). Just bear in mind that something bad could happen if you abort a thread -- if it was writing to a file, then the file is probably corrupt, etc
精彩评论