Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
194
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

 

 

 

 

Module 14 (Optional):

 

Threading and

 

Asynchronous

Contents

Programming

 

 

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

 

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

Presentation:

150 Minutes

Lab:

60 Minutes

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.

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.

vi

Module 14 (Optional): Threading and Asynchronous Programming

Module Strategy

Use the following strategy to present this module:

!Introduction to Threading

Provide a general introduction to the concept of threads and discuss advantages and disadvantages of using them. As a simple illustration of the concept of threading, use Microsoft Internet Explorer to show how you can still do work while waiting for a download operation to complete.

Provide a high level overview of the System.Threading namespace and introduce the asynchronous design pattern, which you will cover in more detail in Asynchronous Programming in .NET.

Explain how application domains play a key role in .NET threading architecture.

!Using Threads in .NET

Focus on the classes in the System.Threading namespace that are used to start, manage, and terminate threads.

In addition to the preceding operations, discuss the role of managed thread local storage.

!Thread Safety

Focus on the issues that students may encounter in multithreaded programming from sharing data and resources between threads, as a result of thread synchronization.

Introduce strategies that the .NET Framework provides for dealing with synchronization, in particular classes and interfaces in System.Threading.

!Special Thread Topics

Introduce the Thread.Timer class, which provides a mechanism for executing methods at specified intervals. Explain how a TimerCallback delegate is used in conjunction with a Timer.

Discuss the use of thread pools in making multiple threads operate more efficiently.

Discuss how managed threads call into a COM object. Outline best practices for implementing thread-safe code.

Module 14 (Optional): Threading and Asynchronous Programming

vii

 

 

 

!Asynchronous Programming in .NET

Provide a brief definition of the concept of asynchronous programming as the ability to issue method calls to other components, and to carry on with other work without waiting for the operation to complete.

Introduce the asynchronous design pattern, and emphasize that one of its innovations is that the caller can decide whether a particular call should be asynchronous.

Spend most of the time in this section on the Asynchronous File Stream Read Example. In addition to the code on the slides and information in the Student Notes, there is an accompanying demonstration.

Discuss the use of Asynchronous delegates to call a synchronous method in an asynchronous manner.

Play the Asynchronous Programming animation to illustrate the .NET Framework common language runtime support for asynchronous programming using Delegate objects.

Module 14 (Optional): Threading and Asynchronous Programming

1

 

 

 

Overview

Topic Objective

To provide an overview of the module topics and objectives.

Lead-in

In this module, you will learn about the support that the

.NET Framework provides for working with multithreaded applications and asynchronous programming.

!Introduction to Threading

!Using Threads in .NET

!Thread Safety

!Special Thread Topics

!Asynchronous Programming in .NET

*****************************ILLEGAL FOR NON-TRAINER USE******************************

In this module, you will learn about the support that the Microsoft® .NET Framework provides for working with multithreaded applications and asynchronous programming. The common language runtime abstracts much of the threading support into classes that greatly simplify most threading tasks. Even if you do not create your own threads explicitly, you need to understand how your code should handle multiple threads if it is run in a multithreaded environment.

You will also learn how to handle thread synchronization to maintain application responsiveness and avoid potential data corruption and other problems.

In the .NET Framework, asynchronous programming is a feature that is supported by Remoting, Networking: HTTP, TCP, File I/O, ASP.NET, and Microsoft Message Queue Server (MSMQ). Because asynchronous programming is a core concept, the .NET Framework provides a common design pattern for handling asynchronous execution. This module introduces the

.NET Framework asynchronous Design Pattern and gives examples of its use.

After completing this module, you 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.

2Module 14 (Optional): Threading and Asynchronous Programming

" Introduction to Threading

Topic Objective

To provide an overview of the topics that you will cover in this section.

Lead-in

In this section, you will learn about the advantages and disadvantages of using threads and the support for threads that is provided by the .NET Framework.

!Overview of Threads

!Overview of Support for Threading in .NET

!Thread Architecture in .NET

*****************************ILLEGAL FOR NON-TRAINER USE******************************

In this section, you will learn about the advantages and disadvantages of using threads and the support for threads that is provided by the .NET Framework.

Соседние файлы в папке c#