Applications With Multiple Threads


The preceding example illustrates the situation in which an application needs to do more than one thing, so the obvious solution is to give the application more than one thread of execution. As mentioned, a thread represents the sequence of instructions that the computer executes. There is no reason why an application should only have one such sequence. In fact, it can have as many as you want. All that is required is that each time you create a new thread of execution, you indicate a method at which execution should start. The first thread in an application always starts at the Main() method because the first thread is started by the .NET runtime, and Main() is the method that the .NET runtime selects. Subsequent threads will be started internally by your application, which means that your application chooses where those threads start.

So far, we have spoken rather loosely about threads happening at the same time. In fact, one processor can do only one thing at a time. If you have a multiprocessor system, it is theoretically possible for more than one instruction to be executed simultaneously — one on each processor. However, for the majority of us who work on single-processor computers, things just don't happen simultaneously. What actually happens is that the Windows operating system gives the appearance of many processes taking place at the same time by a procedure known as pre-emptive multitasking.

Pre-emptive multitasking means that Windows picks a thread in some process and allows that thread to run for a short period of time. Microsoft has not documented the duration of this period, because it is one of those internal operating system parameters that it wants to be free to tweak as Windows evolves in order to maintain optimum performance. In any case, it is not the kind of information you need to know to run the Windows applications. In human terms, this time is very short — certainly no more than milliseconds. It is known as the thread's time slice. When the time slice is finished, Windows takes control back and selects another thread, which will then be allocated a time slice. These time slices are so short that we get the illusion of lots of things happening simultaneously.

Even when your application only has one thread, this process of pre-emptive multitasking is going on because many other processes are running on the system, and each process needs to be given time slices for each of its threads. That's how, when you have lots of windows on your screen, each one representing a different process, you can still click on any of them and have it appear to respond straight away. The response isn't instantaneous — it happens the next time the thread in the relevant process that is responsible for handling user input from that window gets a time slice. However, unless the system is very busy, the wait before that happens is so short that you don't notice it.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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