1. 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
2. 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
3. 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
4. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. 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
12. 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
13. 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
14. 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
15. 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
16. 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
17. 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
18. 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
19. 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
20. 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
21. 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
22. 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
23. 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
24. 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
25. 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
26. 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
27. 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
28. 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
29. Object-Oriented Programming Using C#
Summary
In this session, you learned the text format would be
All the information stored in that:
displayed on of screen as text.
The types a multitasking are:
This means 'A' will be written as 'A' in the files.
• Process-based multitasking
• Thread-based multitasking
Similarly, the number –12345.678 will be written as the
string "-12345.678". multithreading are:
The advantages of
• Improved performance
This means that you can directly display the contents of the
• Minimized system resources usage
file on the screen. access to multiple applications
• Simultaneous
The limitations of multithreading are:
Race condition
Deadlock condition
Lock starvation
Ver. 1.0 Session 16 Slide 29 of 31
30. Object-Oriented Programming Using C#
Summary (Contd.)
– The System.Threading the text format would be
All the information stored in class is used to construct and
displayed on a screen as text. multithreaded application.
access individual threads in a
– Thread priority is the property that in the files.
This means 'A' will be written as 'A'specifies the priority of one
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".shared resource then that resource is used
need to access a
This means that you at a time.
by only one thread can directly display the contents of the
file on the screen.
– The 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
31. Object-Oriented Programming Using C#
Summary (Contd.)
All the informationdomain is athe textprocess inside abe
The application stored in logical format would physical
displayed on a screen as text. the application domain is to
process. The main purpose of
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 numbercalls.
the methods that it –12345.678 will be written as the
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
Editor's Notes
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Students know the importance of surrogate keys. In this session students will learn the strategy to generate the surrogate key. Give an example to explain the strategy to generate the surrogate keys by concatenating the primary key of the source table with the date stamp. For example, data from a Product table has to be loaded into the Product_Dim dimension table on Feb 09, 2006. The product_code is the primary key column in the Product table. To insert the surrogate key values before loading the data into the dimension table, you can combine the primary key value with the date on which the data has to be loaded. In this case the surrogate key value can be product_code+09022006.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.