Object-Oriented Programming Using C#
Objectives
In this session, you will learn to:
Implement multiple threads
Identify the thread priority
Use synchronization in threads
Identify communication between processes
Declare delegates
Instantiate delegate
Ver. 1.0 Session 16 Slide 1 of 31
Object-Oriented Programming Using C#
Demo: Hangman Game
Problem Statement:
The next door children request you to create the Hangman
game for them. The game asks a user to enter a category as
Book or Movie. Based on the category, a book name or movie
name is extracted and the user is asked to guess the name by
giving the character and its position in a string. A user will get
60 seconds to play the game.
Develop the Hangman game application.
Ver. 1.0 Session 16 Slide 2 of 31
Object-Oriented Programming Using C#
Demo: Hangman Game (Contd.)
Solution:
To solve the preceding problem, you need to perform the
following tasks:
1. Identify the data source where the name of the book or movie is
stored.
2. Create a console-based application for the Hangman game.
3. Build and execute the application.
Ver. 1.0 Session 16 Slide 3 of 31
Object-Oriented Programming Using C#
Introducing Multithreading
Multithreading helps to perform various operations
simultaneously and saves users’ time.
Multithreading allows you to achieve multitasking in a
program.
Multitasking is the ability to execute more than one task at
the same time.
Multitasking can be divided into the following categories:
Process-based multitasking
Thread-based multitasking
Let us understand multithreading with the help of an
example.
Ver. 1.0 Session 16 Slide 4 of 31
Object-Oriented Programming Using C#
A lady is working on a computer.
Ver. 1.0 Session 16 Slide 5 of 31
Object-Oriented Programming Using C#
The lady is eating an apple, reading a book, and working on a
computer simultaneously.
Ver. 1.0 Session 16 Slide 6 of 31
Object-Oriented Programming Using C#
Advantages of Multithreading
The advantages of multithreading are:
Improved performance
Minimized system resource usage
Simultaneous access to multiple applications
Program structure simplification
Ver. 1.0 Session 16 Slide 7 of 31
Object-Oriented Programming Using C#
Limitations of Multithreading
The limitations of multithreading are:
Race condition
Deadlock condition
Lock starvation
Ver. 1.0 Session 16 Slide 8 of 31
Object-Oriented Programming Using C#
Creating Multiple Threads
You can create multiple threads in a program by extending
the Thread class.
Ver. 1.0 Session 16 Slide 9 of 31
Object-Oriented Programming Using C#
Identifying the Thread Priority
One of the attributes that controls the behavior of a thread is
its priority.
The .NET Runtime Environment executes threads based on
their priority.
The threads are fixed-priority scheduled. Each thread with
its priority has its position in the thread queue of the
processor.
Ver. 1.0 Session 16 Slide 10 of 31
Object-Oriented Programming Using C#
Defining Thread Priority
Thread priority is the property that specifies the priority of
one thread with respect to the priority of another thread.
Thread priority can be defined as:
Above normal
Below normal
Highest
Lowest
Normal
A thread with higher priority runs before threads, which
have lower priority.
If C# encounters another thread with higher priority, the
current thread is pushed back, and the thread with the
higher priority is executed.
Ver. 1.0 Session 16 Slide 11 of 31
Object-Oriented Programming Using C#
Setting the Thread Priority
You can set the thread priority after it is created using the
Priority property of the Thread class.
The following syntax shows how to set the thread priority:
NewThread.Priority =
ThreadPriority.Highest;
Ver. 1.0 Session 16 Slide 12 of 31
Object-Oriented Programming Using C#
Setting the Thread Priority (Contd.)
NewThread.Priority = ThreadPriority.Highest
ThreadPriority.Highest; Property
Specifies the new
priority setting for a
thread
Ver. 1.0 Session 16 Slide 13 of 31
Object-Oriented Programming Using C#
Setting the Thread Priority (Contd.)
If multiple threads with the same priority are available, the
scheduler cycles through the threads in that priority, giving
each thread a fixed time slice in which to execute.
As long as a thread with a higher priority is available to run,
lower priority threads do not get to execute.
When there are no more runnable threads at a given
priority, the scheduler moves to the next lower priority and
schedules the threads at that priority for execution.
Ver. 1.0 Session 16 Slide 14 of 31
Object-Oriented Programming Using C#
Using Synchronization in Threads
In a multithreaded application, when threads need to share
data with each other, the application should ensure that one
thread does not change the data used by the other thread.
C# enables you to coordinate the actions of multiple threads
by using synchronized methods or synchronized
statements.
Ver. 1.0 Session 16 Slide 15 of 31
Object-Oriented Programming Using C#
Synchronizing Threads
Synchronization of threads ensures that if two or more
threads need to access a shared resource then that
resource is used by only one thread at a time.
You can synchronize your code using the synchronized
keyword.
Synchronization is based on the concept of monitoring. A
monitor is an object that is used as a lock to the data
members and methods of a class.
Ver. 1.0 Session 16 Slide 16 of 31
Object-Oriented Programming Using C#
Synchronizing Threads (Contd.)
The following figure shows how synchronization is
maintained among threads.
Ver. 1.0 Session 16 Slide 17 of 31
Object-Oriented Programming Using C#
Locking Code Using the Monitor Locks
The System.Monitor class enables you to serialize the
access to blocks of code by means of locks and signals.
Ver. 1.0 Session 16 Slide 18 of 31
Object-Oriented Programming Using C#
Using Monitor Locks with the C# Lock Statement
The other way to lock code is by using the C# lock
statement.
Although the C# lock statement does not support the full
array of features found in the Monitor class, it enables you
to obtain and release a monitor lock.
To use the lock statement, simply specify the lock statement
with the code being serialized in braces.
The braces indicate the starting and stopping point of code
being protected.
Ver. 1.0 Session 16 Slide 19 of 31
Object-Oriented Programming Using C#
Communication Between Processes
A process is a running instance of a program.
The communication between the processes at run time
within the same computer or over a network is called the
interprocess communication.
To allow communication between various processes with a
unique address space, the operating system's kernel acts
as the communication channel.
Ver. 1.0 Session 16 Slide 20 of 31
Object-Oriented Programming Using C#
Application Domain
In .NET, threads execute in an application domain. A thread
in one process cannot invoke a method in a thread that
belongs to another process.
In .NET, however, threads can cross the application domain
boundaries, and a method in one thread can call a method
of another application domain.
The application domain is a logical process inside a
physical process.
Ver. 1.0 Session 16 Slide 21 of 31
Object-Oriented Programming Using C#
Application Domain (Contd.)
The following figure shows application domains interacting
with each other.
Ver. 1.0 Session 16 Slide 22 of 31
Object-Oriented Programming Using C#
Application Domain (Contd.)
The main purpose of application domain is to isolate your
applications from other applications.
Application domains run on a single process.
You use the System.AppDomain class to manage
application domains.
Ver. 1.0 Session 16 Slide 23 of 31
Object-Oriented Programming Using C#
Introducing Delegates
Delegates in C# allow you to dynamically change the
reference to the methods in a class.
A delegate is a reference type variable, which holds the
reference to a method.
Delegates are a general-purpose mechanism for indirectly
calling methods at runtime. Primary use of delegates in C#
programming is for implementing events and the call-back
methods.
To implement delegates in your application you need to
declare delegates, instantiate delegates and use delegates.
Ver. 1.0 Session 16 Slide 24 of 31
Object-Oriented Programming Using C#
Declaring Delegates
The methods that can be referenced by a delegate are
determined by the delegate declaration.
The delegate can refer to the methods, which have the
same signature as that of the delegate.
Ver. 1.0 Session 16 Slide 25 of 31
Object-Oriented Programming Using C#
Declaring Delegates (Contd.)
Following is the syntax of delegate declaration:
delegate<return type><delegate-
name>(<parameter list>)
Ver. 1.0 Session 16 Slide 26 of 31
Object-Oriented Programming Using C#
Instantiating Delegates
Assign the address of the required method to the delegate
object.
This can be done by calling the constructor of the delegate
class and passing the method name.
Ver. 1.0 Session 16 Slide 27 of 31
Object-Oriented Programming Using C#
Instantiating Delegates (Contd.)
The following example shows how to assign the address of a
method to a delegate variable:
public void DelegateFunction(string PassValue)
{
// Method implementation Here
}
//Delegate Declaration
public delegate void MyDelegate(string
ArgValue);
public void UseMethod()
{
//Delegate Instantiation
MyDelegate DelegateObject = new
MyDelegate(DelegateFunction);
}
Ver. 1.0 Session 16 Slide 28 of 31
Object-Oriented Programming Using C#
Summary
Allthis
In the session,
information
youstored
learnedin the
that:text format would be
displayed on of
The types a screen as text.
multitasking are:
This means 'A' will be
Process-based written as 'A' in the files.
multitasking
Thread-based multitasking
Similarly, the number –12345.678 will be written as the
The"-12345.678".
string advantages of multithreading are:
Improved performance
This means that you can directly display the contents of the
Minimized system resources usage
file on the screen.
Simultaneous access to multiple applications
The limitations of multithreading are:
Race condition
Deadlock condition
Lock starvation
Ver. 1.0 Session 16 Slide 29 of 31
Object-Oriented Programming Using C#
Summary (Contd.)
Theinformation
All the stored in class
System.Threading is used
the text to construct
format would be and
access on
displayed individual
a screen threads in a multithreaded application.
as text.
ThisThread
means priority is be
'A' will the written
propertyas that
'A'specifies the priority of one
in the files.
thread with respect to the priority of another thread.
Similarly, the number –12345.678 will be written as the
Synchronization of threads ensures that if two or more threads
string "-12345.678".
need to access a shared resource then that resource is used
Thisbymeans that
only one you at
thread can directly display the contents of the
a time.
file on
Thethe screen.
System.Monitor class enables you to serialize the
access to blocks of code by means of locks and signals.
The communication between processes at runtime within the
same computer or over a network is called interprocess
communication.
Ver. 1.0 Session 16 Slide 30 of 31
Object-Oriented Programming Using C#
Summary (Contd.)
Theinformation
All the application domain
storedisinathe
logical
textprocess
format inside
wouldabephysical
process.onThe
displayed main purpose
a screen of the application domain is to
as text.
isolate your applications from the other applications.
This means 'A' will be written as 'A' in the files.
Delegates allow you to write code that can dynamically change
Similarly, the number
the methods –12345.678 will be written as the
that it calls.
string "-12345.678".
This means that you can directly display the contents of the
file on the screen.
Ver. 1.0 Session 16 Slide 31 of 31