Chapter 3: Threading and the Asynchronous Pattern


Overview

In the last chapter, we saw how blocking I/O is performed on different streams, including network streams. However, blocking I/O is problematic because the I/O calls might do exactly what their name implies ”block for an unknown amount of time. If an application does anything besides I/O operations, such as servicing a user interface, calling a blocking method will freeze the user interface and provide for a bad user experience. You can solve this problem with the Microsoft Windows .NET Framework concurrent programming model by using threads or by using the asynchronous programming pattern.

Threads allow you to have one or more execution points in a program that can operate at the same time. When using threads, a thread can be spawned to perform any blocking I/O operations while your main application is freed to do other tasks , such as service the user interface. When a new thread is created, it begins executing from a method within your application. This method then executes all other threads in the application in parallel, including the main application thread.

The asynchronous programming pattern, on the other hand, allows many of the classes in the .NET Framework to perform operations concurrently. With the asynchronous programming pattern, you can post operations such as a read or write to be asynchronously handled by the underlying class object, and the object will signal the application when the operation has completed. This way, the application can post a number of operations concurrently, which will not block, and your main application can perform other actions such as updating the user interface and responding to user events.

This chapter introduces threads and the asynchronous pattern, which can enable you to design great networking applications. First we ll describe the threading concept by showing how to create and manage threads to perform concurrent programming in an application where one or more threads can execute code while your main program continues to execute. Then we ll describe thread synchronization concepts that are important to understand when using threads because of their concurrent programming behavior. Finally, after threads are described, we ll talk about the asynchronous programming pattern that s common to many classes in the .NET Framework and is similar in concept to the threading model.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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