Overview


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 actually 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 shows the 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 waits for a request from a client, the listener thread. 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 image from book 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 18-1 use multiple threads. You can see one instance of Internet Explorer running 51 threads.

image from book
Figure 18-1

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 virtual memory 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 virtual memory 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’s usually just fine to use managed threads.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net