8

I am trying to figure out what is the purpose of the threads that appear in every new c# application. I created a new console application with empty Main function:

static void Main(string[] args)
{
}

and put a break-point on the end of the function, then I looked on the threads window:

Default Threads

Sometimes it shows 8 threads and sometimes 7 threads.

Can anyone explain what is the purpose of all those threads, and why do I need them for such a simple project?

4

2 回答 2

6

In brief, these extra threads are GC, Finalizer, VS, and Debugger related. The link below provides a more detailed answer to your question:

Why does this simple .NET console app have so many threads?

于 2013-10-18T13:38:05.047 回答
2

First of all I think we need to understand what are threads?

Threads:

Threading enables your program to perform concurrent processing so that you can do more than one operation at a time. For example, you can load a heavy images to your application, perform background tasks, and at that time you can handle a streaming to files.

If you would not use threads - then when you were loading the images to your application then your UI were stucked so you couldn't do nothing else, just waiting until the images will finish loading.

So why our application starts at 7-8 threads?

So lets see which threads we have:

By default, a C# program has one thread. This thread executes the code in the program starting and ending with the Main method.

You have also a garbage collector thread which responsible for killing objects when their life cycle ends.

And there are some more threads of debugging.

于 2013-10-18T13:40:57.347 回答