What are the differences between these two types of threads and foreground ?
What are the differences between these two types of threads and foreground ?
The only difference is that background threads do not determine the life of the execution environment, they do not hold an application running, and depend on the foreground threads to continue existing. If all foreground threads end, the runtime terminates independently if there are still background threads running, evidently closing these as well. And this has some implications in the way it is communicated to the application.
In general threads run in foreground . This is the default . Only when you run a thread that exists in function of the others in foreground should it be set to background . Threads monitors are a good example.
Threads managed by ThreadPool are background by default .
Example demonstrating the operation.
Source: Microsoft documentation .
Background threads are identical to foreground threads with one exception: a Background Thread does not keep the managed execution environment running. That is: the difference is that one¹ does not prevent the application from being terminated, the other², yes.
¹ Background Thread.
² Foreground Thread.
The correct thing, regardless of the type of thread, is to be careful about your threads, to prevent them from getting out of control and the application has unexpected results.
Source:
Background threads are identical to foreground threads with one exception: the background thread does not keep the managed execution environment running.