views:

114

answers:

3

Hi folks, what's the difference between background, foreground & main threads? What are the diff types of threads in .NET?

TIA

+5  A: 

The distinction is succinctly stated in the documentation. Background threads are interrupted when the program ends.

http://msdn.microsoft.com/en-us/library/h339syd0(VS.71).aspx

BC
Nice link +1 :)
John Weldon
A: 

A background thread is exactly that, it is a thread that is running in the background from the UI thread of an application. The UI thread in something like a winforms application is the thread responsible for repainting the UI and other user interactions.

Moving long running processes off to a background thread will help improve usability. Here is a good intro document to threading for you.

Mitchel Sellers
+3  A: 

A background thread (whose Thread object has the Background property set to true) will not prevent an application from quitting.

Once all normal threads have exited, any running background threads are immediately terminated. In addition, if an AppDomain is unloaded, all background threads in the AppDomain are immediately aborted.

The threads managed by the ThreadPool are background threads.

A foreground thread is an ordinary thread.

The main thread is the initial thread that started the program. (The thread running the Main method)

For more information, see here.

SLaks
SLaks, is there a limit to number of threads in threadpool ?
SoftwareGeek
Yes, there is; read the link. The default limit is 250 per CPU; you can change it by calling `ThreadPool.SetMaxThreads`.
SLaks
I might be mistaken, but I thought the limit was per core. Another point, there's very little chance you need that much threads in your app. All you're going to do is adding threading overhead. From experience 2/3 threads by core seems like a good choice.
Sylvestre Equy
@Sylvestre: You are correct on both counts.
SLaks