Contents
Overview 1
Introduction to Threading 2
Using Threads in .NET 9
Thread Safety 29
Special Thread Topics 51
Asynchronous Programming in .NET 74
Lab 14: Working With Multithreaded
Applications 92
Review 108
Module 14 (Optional):
Threading and
Asynchronous
Programming
Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, places or events is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
2001-2002 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint,
Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and
Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A.
and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.
Module 14 (Optional): Threading and Asynchronous Programming iii
Instructor Notes Module 14
This module provides students with knowledge about the support that the
Microsoft
®
.NET Framework provides for working with multithreaded
applications and asynchronous programming.
After completing this module, students will be able to:
!
Create and manage threads.
!
Create thread-safe code.
!
Create and use timers.
!
Create threads using thread pools.
!
Create managed threads that interact well with COM components.
!
Create Microsoft Windows
®
Forms applications with background threads.
!
Make asynchronous calls using delegates.
Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
Required Materials
To teach this module, you need the Microsoft PowerPoint® file 2349B_14.ppt.
Preparation Tasks
To prepare for this module:
!
Read all of the materials for this module.
!
Practice the demonstrations.
!
Complete the lab.
Presentation:
150 Minutes
Lab:
60 Minutes
iv Module 14 (Optional): Threading and Asynchronous Programming
Demonstrations
This section provides demonstration procedures that will not fit in the margin
notes or are not appropriate for the student notes.
The code for each of the following demonstrations is contained in project
folders that are located in <install folder>\Democode\Mod14.
Use the debugger to step through the code while you point out features and ask
students what they think will happen next.
Managing Threads
In this demonstration, you will show students how to create and manage threads
in the .NET Framework by using some of the classes and methods that were
covered in Using Threads in .NET.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.1.
Interrupt and Abort
In this demonstration, you will show students how to terminate threads by using
the Thread.Interrupt and Thread.Abort methods.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.2.
Using Synchronization Contexts and Synchronized Code
Regions to Provide Thread Safety
In this demonstration, you will use a series of tests to show students how to
achieve thread safety through the use of synchronization contexts and
synchronized code regions.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.3.
Using Synchronization Techniques and Thread Pooling
In this demonstration, you will show students how to perform synchronization
by using the C# lock keyword and manual synchronization primitives, and also
how to obtain multiple threads by using a thread pool.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.4.
Module 14 (Optional): Threading and Asynchronous Programming v
Windows Forms Threading
In this demonstration, you will show students how to use a background thread
in a Windows Forms application.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.5.
Asynchronous File Stream Read
In this demonstration, you will show students how to use synchronous and
asynchronous read methods on a file stream. For the asynchronous case, you
will show four different ways to complete the operation: callback, poll, end
method call, and wait with timeout.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.6.
Using a Delegate
In this demonstration, you will show students how to use a delegate object to
make asynchronous calls.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod14\Demo14.7.
Multimedia Presentation
This section lists the multimedia items that are part of this module. Instructions
for launching and playing the multimedia are included with the relevant slides.
Asynchronous Programming
This animation illustrates the .NET Framework common language runtime
support for asynchronous programming using Delegate objects.