0% found this document useful (0 votes)
10 views14 pages

PRG Chapter 5

This document provides an overview of multithreaded, asynchronous, and parallel programming in C#. It discusses threads, tasks, and how to make the UI responsive using async and await. Threads control program execution flow and can be created using the Thread class. Tasks manage units of work on thread pool threads and provide features like returning results and chaining tasks. Async and await allow asynchronous implementation to make the UI responsive.

Uploaded by

Redsadow 238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

PRG Chapter 5

This document provides an overview of multithreaded, asynchronous, and parallel programming in C#. It discusses threads, tasks, and how to make the UI responsive using async and await. Threads control program execution flow and can be created using the Thread class. Tasks manage units of work on thread pool threads and provide features like returning results and chaining tasks. Async and await allow asynchronous implementation to make the UI responsive.

Uploaded by

Redsadow 238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 5

Multithreaded, Async & Parallel Programming


Module Objectives

1.Working with Threads

2.Working with Task

3.Making UI Responsive (async and await)

4.Using Parallel Programming


Working with Threads
• A thread controls the flow of an executable program. By default, a
program has one thread called Main Thread. Main Thread starts when
control enters in the Main method and it terminates when Main method
returns.

• If the execution of a program is controlled by more than one thread, it’s


called a Multithreaded Application. Such a program increases the
performance and response time of an application.

• In C#, the System.Threading namespace is used for creating and


managing thread(s) in a multithreaded application. A thread can be
created by using System.Threading.Thread class.

• A thread can only be manipulated on a method. For example,


MainThread needs a Main method to control the flow of a progam.
States of a Thread

• In a C# program, a thread can be found in any of the


following states :
Create and Start a Thread

• Inside the MainThread, a thread can be initialized by using the Thread


class of the System.Threading namespace. A thread can start its
execution when a Thread.Start() method is called.
Create and Start a Thread
Managing Threads
• The following example demonstrates the use of the sleep() method for
making a thread pause for a specific period of time.
Destroying Threads
• The Abort() method is used for destroying threads.
Working with Tasks
• Task is a lightweight object which asynchronously manages the unit of work. Task
doesn’t create new threads. Instead it efficiently manages the threads of a
threadpool. Tasks are executed by TaskScheduler, which queues tasks onto threads.

Task provides the following powerful features over thread and threadpool.
1.Task allows you to return a result.
2.It gives better programmatical control to run and wait for a task.
3.It reduces the switching time among multiple threads.
4.It gives the ability to chain multiple tasks together and it can execute each task one
after the other by using ContinueWith().
5.It can create a parent/child relationship when one task is started from another task.
6.Task can cancel its execution by using cancellation tokens.
7.Task leaves the CLR from the overhead of creating more threads; instead it implicitly
uses the thread from threadpool.
8.Asynchronous implementation is easy in task, by using “async” and “await”
keywords.
9.Task waits for all of the provided Task objects to complete execution.
Methods and Properties of Task Class
Create and start a Task
Working with Tasks
Summary
 A thread controls the flow of an executable program.
 By default, a program has one thread called Main Thread.
Main Thread starts when control enters in the Main method
and it terminates when Main method returns.
 Application has two kinds of threads: Background Thread
and Foreground Thread.
 Background thread doesn’t hold the main thread to finish
its execution. If the main thread completes its execution it
will terminate the progam.
 Foreground thread does hold the main thread to terminate
the progam until foreground completes its execution.
 Threadpriority defines how much CPU time a thread will
have for execution.
 Task doesn’t create new threads. Instead it efficiently
manages threads of a threadpool.
PRG521
Main | CH5
Heading | Subheading

The End

2 0 2 22 0C2T0U CTTr U
a i nTirnagi nSi nogl u St ioolnust i o|n sA l |l RAi gl l hRt si gRhet s eRr ev es edr v |e dc t u| t rcat iuntirnagi n. ai nc g
. z. a c . z a

You might also like