A thread is an independent stream of instructions in a program. All your C# programs up to this point have one entry point – the Main () method. Execution starts with the first statement in the Main ( ) method and continues until that method returns. • This program structure is all very well for programs, in which there is one identifiable sequence of tasks, but often a program needs to do more than one thing at the same time. Threads are important both for client-side and for server-side applications.
While you type C# code in the Visual Studio editor, the Dynamic Help window immediately showsthe topics that fit to the code you type. A background thread is searching through help. The same thing is done by the spell checker in Microsoft Word. One thread is
waiting for input from the-user, while the other does some background research. A third thread can store the written data in an interim file, while another one downloads some additional data from the Internet. In an application that is running on the server, one thread, the listener thread, waits for a request from a client. As soon as the request comes in, the request is forwarded to a separate worker thread, which continues the communication with the client. The listener thread immediately comes back to get the next request from the next client.
With the Windows Task Manager, you can turn on the column Threads from the menu View c:> Select Columns and see the processes and the number of threads for every process. Only cmd. exe is running inside a single thread; all the other applications shown in Figure 19-1 use multiple threads. You can see one instance of Internet Explorer running 51 threads.
A process contains resources, such as Window handles, handles to the file system, or other kernel objec’s. Every process has virtual memory allocated. A process contains at least one thread. The operating system schedules threads. A thread has a priority, a program counter for the program location where it is actually processing, and a stack to store its local variables. Every thread has its own stack, but the memory for the program code and the heap are shared among all threads of a single process. This makes
communication among threads of one process fast – the same virtualenernory is addressed by all threads of a process. However, this also makes things difficult because multiple threads can change the same memory location.
A process manages resources that include virtual1nemory and Window handles, and contains at least one thread. A thread is required to run the program. With .NET, a managed thread is defined by the Thread class. A managed thread does not necessarily map
to one operating system thread. This can be the case, but it is the work of the .NET runtime host to map managed threads to the physical threads of the operating system, Here, the runtime host of SQL Server 2005 behaves very differently from the runtime host for Windows applications. You can get . information about the native thread with the ProcessThread class, but with managed applications, it is usually just fine to use managed threads.