Multi-Threading Techniques

Let's say you've decided you are definitely going to use multithreading techniques in an application. There are basically five options available. We'll discuss each of these briefly, and then spend most of the rest of chapter presenting samples that use the main techniques.

Asynchronous Delegate Invocation

Generally speaking, asynchronous delegate invocation is the best technique if you have a number of simple and independent tasks that you want to hand out to another thread. The advantage of this technique is that a thread pool thread will be used to execute the asynchronous operation so you get all the performance benefits of the thread pool. If your application needs to do a lot of asynchronous operations then this performance benefit can be considerable as threads get automatically reused. Not only that, but literally any method can be wrapped in a delegate and invoked asynchronously. All the support for this is already built into delegates by the CLR - you don't need to add any extra support for this in your method whatsoever! That's a powerful argument for this technique.

Explicitly Create Your Own Threads

Explicitly creating your own threads will be the technique of choice if you need a small number of threads running for a long time and interacting with each other. By explicitly creating your own threads, you don't get to use the thread pool - but as we saw earlier you do get Thread references to the threads created, which means you have extra control over the threads. You also get to determine exactly which thread an operation is executed on - something that you cannot do with the thread pool, since the CLR picks the thread for each asynchronous operation. This will be significant if you need to guarantee that several operations will be executed on the same thread.

Timers

A timer will be the technique of choice if you need to perform some processing at regular repeated intervals - for example polling an object to see what its state is every few seconds. The System.Threading.Timer object that we will use in this chapter uses the thread pool to make callbacks at regular intervals (note that System.Threading.Timer is not a UI-based timer and so is highly accurate).

Built-In Asynchronous Support

Taking advantage of existing asynchronous support is not something that we'll go into in detail in this chapter. Basically, there are certain .NET Framework classes, such as Stream and WebRequest, which have intrinsic support for requesting that certain operations can be carried out asynchronously. The pattern for invoking this support closely follows the pattern for asynchronous delegate invocation, but the underlying implementation may be different. We'll briefly mention some of the classes that support this pattern when we cover asynchronous delegates.

Explicitly Queue Items to the Thread Pool

Queuing an item to the thread pool is also something that we're not going to cover in detail in this chapter, although I presented the basic syntax for doing this earlier. It has a very similar effect in practice to asynchronous delegate invocation.



Advanced  .NET Programming
Advanced .NET Programming
ISBN: 1861006292
EAN: 2147483647
Year: 2002
Pages: 124

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