Thứ Bảy, 22 tháng 2, 2014

Tài liệu Module 14 (Optional): Threading and Asynchronous Programming pptx

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
!
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.

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.
2 Module 14 (Optional): Threading and Asynchronous Programming



"
""
"

Introduction to Threading
!
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.
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.
Module 14 (Optional): Threading and Asynchronous Programming 3


Overview of Threads
!
Threads
#
The basic unit to which an operating system allocates processor time
#
Enable multiple activities to appear to occur simultaneously
!
Advantages of using multiple threads
#
Application does background processing while keeping the UI responsive
#
Distinguish tasks of varying priority
#
Communicate over a network, to a Web server, and to a database
!
Potential disadvantages of using threads
#
Diminished performance due to increased operating system overhead, for
example, thread context switching
#
Controlling code execution with many threads is complex, and can be a
source of many difficult to find and fix bugs

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Whether you are developing for computers with one processor or several, you
want your application to provide the most responsive interaction with the user,
even if the application is currently doing other work. Using multiple threads of
execution is one of the most powerful ways to keep your application responsive
to the user and at the same time make use of the processor in between or even
during user events.
Threads
Threads are the basic unit to which an operating system allocates processor
time, and more than one thread can execute code inside a process. Each thread
maintains exception handlers, a scheduling priority, and a set of structures that
the system uses to save the thread context until it is scheduled. The thread
context includes all of the information that the thread needs to smoothly resume
execution, including the thread’s set of CPU registers and stack, in the address
space of the thread’s host process.
Operating systems use processes to separate the different applications that they
are executing. The .NET Framework further subdivides an operating system
process into lightweight, managed subprocesses, called application domains,
represented by System.AppDomain. One or more managed threads,
represented by System.Threading.Thread, can run in one or any number of
application domains within the same process. A preemptive multitasking
operating system allocates a processor time slice to each thread that it executes.
Because each time slice is small, multiple threads appear to execute at the same
time.
Topic Objective
To define threads in the
context of general
multitasking and state the
primary advantages and
disadvantages of using
threads.
Lead-in
Whether you are developing
for computers with one
processor or several, you
want your application to
provide the most responsive
interaction with the user,
even if the application is
currently doing other work.
Delivery Tip
You can quickly illustrate
how multiple threads are
used to create nonblocking
UI by running the Microsoft
Internet Explorer Web
browser. Start the browser
and type a URL address or
click a link to a site that
takes some time to
download. Show how you
can still interact with the
application during this
download operation, for
example, canceling the
current download by clicking
Stop.
4 Module 14 (Optional): Threading and Asynchronous Programming


Advantages of using multiple threads
Threads enable multiple activities to appear to occur simultaneously in an
application. For example, a user can edit a worksheet while another thread
recalculates other parts of the worksheet within the same application. Threading
maintains the responsiveness of the user interface while background processing
is occurring. Threads can be used to enable users to receive notification of a
task’s progress, and even to cancel the task at any time.
You can also use threads to distinguish tasks of varying priority. For example,
you can use a high-priority thread to manage time-critical tasks, and a low-
priority thread to perform other tasks.
Threads are also useful when an application must wait for an event, such as user
input or a read from the network or from a file, before continuing to execute.
Multiple threads enable the processor to handle a separate task while waiting
for the completion of the event.
Potential disadvantages of using threads
In some circumstances, threading may cause application performance to
degrade. On a single-processor computer, a compute-bound task, such as
calculating a series of values by using multiple threads, would be slower
because of the overhead caused by thread-switching. Keeping track of a large
number of threads consumes significant processor time. If there are too many
threads, most of them will not make significant progress.
Controlling code execution with many threads is complex, and can be a source
of many bugs. You run the risk of data corruption or other problems, such as
deadlocks and race conditions. To protect an application’s data from possible
corruption, you must ensure that access to shared data is properly synchronized.
For more information about deadlocks and race conditions, see Thread Safety in
this module.

While this module focuses on threading, there are other ways of
achieving concurrency that include using multiple processes/AppDomains,
messaging, and database stored procedures.

Note
Module 14 (Optional): Threading and Asynchronous Programming 5


Overview of Support for Threading in .NET
!
Basic thread namespace
#
System.Threading
!
Standard design pattern for asynchronous
programming
#
Hides thread creation and synchronization details
#
Supported by .NET Framework delegate classes and/or
the IAsyncResult interface

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
To provide support for multithreaded programming, the .NET Framework
supplies its own namespace for thread creation and management and a
programming model to handle asynchronous operations.
Basic thread namespace
The System.Threading namespace provides classes and interfaces that enable
multithreaded programming. System.Threading includes classes that are used
to create and manage threads, protect shared data, and improve system
responsiveness.
The System.Threading.Thread class is an abstraction of a managed thread that
executes within the runtime. This includes threads that are created by the
runtime and those that are created outside the runtime but that interact with the
runtime environment to execute some managed code.
The System.Threading namespace includes classes that assist with thread
synchronization and protection of shared data. For example, the
System.Threading.Interlocked class provides atomic operations for variables
that are shared by multiple threads. The System.Threading.Monitor class
provides a synchronization mechanism to ensure that where multiple threads
access a shared resource, only one thread can access the resource at a particular
time.
Topic Objective
To provide a high-level
overview of the
System.Threading
namespace and introduce
the asynchronous design
pattern.
Lead-in
Before examining how
threads work in the .NET
Framework, let’s briefly
review important ways in
which the .NET Framework
provides support for
threading.
6 Module 14 (Optional): Threading and Asynchronous Programming


Standard design pattern for asynchronous programming
The standard asynchronous design pattern in the .NET Framework provides an
efficient model to manage asynchronous operations and provide a consistent
programming model. The .NET Framework provides support for asynchronous
programming using the IAsyncResult interface and delegate classes that enable
a programmer to avoid some of the implementation details of threading. The
asynchronous design pattern is covered in more detail later in this module.
For more information about the System.Threading namespace and its classes,
see System.Threading Namespace in the .NET Framework SDK
documentation.

Không có nhận xét nào:

Đăng nhận xét