What are the differences between background and foreground threads?

8

What are the differences between these two types of threads and foreground ?     

asked by anonymous 17.09.2014 / 21:36

2 answers

7

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 .

    
17.09.2014 / 21:41
5

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.

link

    
17.09.2014 / 21:40