Object-Oriented Programming Using C#
Objectives


                In this session, you will learn to:
                   Handle exceptions
                   Implement the user-defined exceptions
                   Implement threads
                   Define the life cycle of a thread




     Ver. 1.0                        Session 14            Slide 1 of 30
Object-Oriented Programming Using C#
Handling Exceptions


                In exception handling, the application is divided into blocks
                of code.
                A block that shows the probability of raising an error
                contains one or more exception handlers.
                The exception handlers follow a control structure and a
                uniform way of handling the system level and application
                level errors.
                The blocks for exception-handling can be implemented
                using the following keywords:
                   try
                   catch
                   finally


                Let us look at each of these keywords in detail.

     Ver. 1.0                       Session 14                         Slide 2 of 30
Object-Oriented Programming Using C#
Handling Exceptions (Contd.)


                The try block:
                   The try block guards statements that may throw an exception.
                   Following is the syntax of try block statement:
                    try
                    {
                    //statements that may cause an exception
                    }

                   The try block governs statements that are enclosed within it
                   and defines the scope of the exception-handlers associated
                   with it.
                   A try block must have at least one catch block.




     Ver. 1.0                       Session 14                            Slide 3 of 30
Object-Oriented Programming Using C#
Handling Exceptions (Contd.)


                The catch block:
                   The catch statement of the catch block takes an object of the
                   exception class as a parameter, which refers to the raised
                   exception.
                   You can associate an exception-handler with the try block by
                   providing one or more catch handlers, immediately after the try
                   block:
                     try
                     {
                         //statements that may cause an exception
                     }
                     catch (…)
                     {
                       //error handling code
                     }

     Ver. 1.0                       Session 14                            Slide 4 of 30
Object-Oriented Programming Using C#
Handling Exceptions (Contd.)


                The finally block:
                   The finally block is used to execute a given set of statements,
                   whether an exception is thrown or not thrown:
                    try
                    {
                         //statements that may cause an exception
                    }
                    catch (…)
                    {
                      //error handling code
                    }
                    finally
                    {
                      //statements to be executed
                    }
     Ver. 1.0                        Session 14                            Slide 5 of 30
Object-Oriented Programming Using C#
Demo: Handling Exception for Arrays Beyond Limit


                Problem Statement:
                   David is working on a project where he is calculating the sum
                   of values in an integer array. David needs to handle the
                   exceptions, which can occur while he is working with the
                   arrays. If any exceptional condition is reached when David is
                   executing the application, the application needs to display an
                   exception message.
                   Help David to handle the exceptions.




     Ver. 1.0                       Session 14                             Slide 6 of 30
Object-Oriented Programming Using C#
Demo: Handling Exception for Arrays Beyond Limit (Contd.)


                Solution:
                   To develop a console-based application, David need to
                   perform the following tasks:
                    1. Create a console-based application.
                    2. Build and execute an application.




     Ver. 1.0                        Session 14                            Slide 7 of 30
Object-Oriented Programming Using C#
Implementing the User-Defined Exceptions


                In C#, you can create your own exception class. Such kinds
                of exceptions are known as user-defined exceptions.
                The Exception class is the base class for all the
                exceptions in C#.
                The user-defined exception classes must follow the
                hierarchy of either the exception class or of one of the
                standard inherited classes.




     Ver. 1.0                     Session 14                       Slide 8 of 30
Object-Oriented Programming Using C#
Implementing the User-Defined Exceptions (Contd.)


                User-defined exception classes are derived from the
                ApplicationException class.
                To implement user-defined exceptions, you need to:
                – Raise your exception: You can use the throw statement to
                  raise your own exceptions.
                – Throw an object: You can throw an object if the object is
                  either directly or indirectly derived from System.Exception.
                  You can use a throw statement in the catch block to throw the
                  present object, as shown in the following code:
                  catch(Exception caught)
                    {
                      . . .
                       throw caught
                    }


     Ver. 1.0                      Session 14                           Slide 9 of 30
Object-Oriented Programming Using C#
Implementing Threads


               A thread is defined as the execution path of a program.
               You can define a unique flow of a control in a program,
               using a thread.
               Threads are used to run applications that perform large and
               complex computations.
               A process that is executed using one thread is known as a
               single-threaded process, where the process is a running
               instance of a program.
               Single-threaded application can perform only one task at a
               time. You have to wait for one task to complete before
               another task can start.




    Ver. 1.0                      Session 14                       Slide 10 of 30
Object-Oriented Programming Using C#
Implementing Threads (Contd.)


                  The following figure shows a single-threaded process.




                • To execute more than one task at a time, you can create
                  multiple threads in a program.
                • A process that creates two or more threads is called a
                  multithreaded process.




     Ver. 1.0                        Session 14                      Slide 11 of 30
Object-Oriented Programming Using C#
Implementing Threads (Contd.)


                The following figure shows a multithreaded process.




     Ver. 1.0                     Session 14                          Slide 12 of 30
Object-Oriented Programming Using C#
The Thread Model in C#


                In single-threaded systems, an approach called event loop
                with polling is used.
                Polling is the process in which a single event is executed at
                a time.
                In the event loop with polling approach, a single thread runs
                in an infinite loop till its operation is completed.
                In a single-threaded application if the thread is suspended
                from execution because it is waiting for a system resource,
                the entire program stops executing.
                In multithreading, the time for which a thread waits for the
                CPU time can be utilized to perform another task.




     Ver. 1.0                      Session 14                        Slide 13 of 30
Object-Oriented Programming Using C#
The Thread Model in C#(Contd.)


                • In C#, you will use the Thread class to work with threads.
                • The System.Threading.Thread class is used to
                  construct and access individual threads in a multithreaded
                  application.




     Ver. 1.0                        Session 14                       Slide 14 of 30
Object-Oriented Programming Using C#
The Main Thread


                • The main thread is created automatically on the start up of a
                  C# program execution.
                • The threads which are created exclusively using the
                  Thread class are called as child threads, where the main
                  thread is called a parent thread or a primary thread.
                • You can access a thread using the CurrentThread
                  property of the Thread class.




     Ver. 1.0                         Session 14                       Slide 15 of 30
Object-Oriented Programming Using C#
Working with Threads


                • In C#, you create a thread by creating an object of type
                  Thread, giving its constructor a ThreadStart reference,
                  and calling the new thread’s Start() method.
                • The new thread starts executing asynchronously with an
                  invocation of the thread’s method.
                • There are various methods available with the Thread class.
                  Using these methods, you can control the execution of
                  threads. Few of these methods are:
                   – Start(): Starts a thread.
                   – Sleep(): Makes the thread to pause for a period of time.
                   – Abort(): Terminates the thread.
                   – Suspend(): Suspends a thread. If the thread is already
                     suspended it has no effect.
                   – Resume(): Resumes the suspended thread.


     Ver. 1.0                         Session 14                          Slide 16 of 30
Object-Oriented Programming Using C#
Creating Threads


                • You can create threads by extending the Thread class.
                • The extended thread class calls the Start() method to
                  begin the child thread execution. Following is an example of
                  creating threads:
                   ThreadStart ChildRef = new
                   ThreadStart(ChildThreadCall);
                   Thread ChildThread = new Thread(ChildRef);
                   ChildThread.Start();




     Ver. 1.0                         Session 14                      Slide 17 of 30
Object-Oriented Programming Using C#
Managing Threads


               •   There are many tasks you might need to perform to manage
                   the activity or life of a thread.
               •   You can manage all these tasks by using the various thread
                   methods available with the Thread class.
               •   The static Thread.Sleep() method calls the static
                   CurrentThread method, which then pauses that thread
                   for the specified amount of time.




    Ver. 1.0                         Session 14                      Slide 18 of 30
Object-Oriented Programming Using C#
Destroying Threads


                •   If the thread is required to be destroyed, the
                    Thread.Abort() method will allow you to accomplish the
                    task.
                •   The runtime aborts the thread by throwing a
                    ThreadAbortException. This exception cannot be
                    caught.
                •   If the finally block is present in the method, the runtime
                    will send the control to it.




     Ver. 1.0                         Session 14                       Slide 19 of 30
Object-Oriented Programming Using C#
Thread Life Cycle


                •   The lifecycle of a thread starts when an object of the
                    System.Threading.Thread class is created. The life
                    cycle of the thread ends with task execution.
                •   There are various states in the life cycle of a thread. These
                    states are:
                       The Unstarted state
                       The Runnable state
                       The Not Runnable state
                       The Dead state


                    Let us understand the life cycle of the thread with the help
                    of the following figure.



     Ver. 1.0                           Session 14                        Slide 20 of 30
Object-Oriented Programming Using C#




              Start()   Started




                        Work Completed   Stopped




   Ver. 1.0                 Session 14             Slide 21 of 30
Object-Oriented Programming Using C#




              Start()   Started                Suspend()   Suspended




                                                           Resume()




   Ver. 1.0                       Session 14                      Slide 22 of 30
Object-Oriented Programming Using C#



              Start()   Started




                                    Sleep()     Wait/Join
                                                Sleep




                                                Interrupt()



                                               Time Expires




   Ver. 1.0                       Session 14                  Slide 23 of 30
Object-Oriented Programming Using C#




              Start()   Started




                                                          Thread Responds to
                         Abort()           Stop Request   Stop Request




                                                               Stopped




   Ver. 1.0                        Session 14                         Slide 24 of 30
Object-Oriented Programming Using C#
The Unstarted State


                • When an instance of the Thread class is created, the
                  thread enters the unstarted state.
                • A new thread is an empty object of the Thread class, and
                  no system resources such as memory are allocated to it.




     Ver. 1.0                        Session 14                     Slide 25 of 30
Object-Oriented Programming Using C#
The Runnable State


                • The thread remains in the unstarted state until the program
                  calls the Start() method of the Thread class, which
                  places the thread in the runnable state and immediately
                  returns control to the calling thread.
                • This state is also called as the ready or started state.
                • The newly started thread and any other threads in the
                  program execute concurrently.




     Ver. 1.0                         Session 14                      Slide 26 of 30
Object-Oriented Programming Using C#
The Not Runnable State


                A thread is not in the runnable state if it is:
                   Sleeping
                   Waiting
                   Blocked




     Ver. 1.0                        Session 14                   Slide 27 of 30
Object-Oriented Programming Using C#
The Dead State


                • A running thread enters the dead state when the statements
                  of the threads method are complete. This state is also called
                  the terminated state.
                • A program can force a thread into the dead state by calling
                  the Abort() method of the Thread class on the
                  appropriate thread object.




     Ver. 1.0                         Session 14                       Slide 28 of 30
Object-Oriented Programming Using C#
Summary


               In this session, you learned that:
                  Exception handling is implemented using the following
                  keywords:
                    • try
                    • catch
                    • finally
                – Exception-handling provides a structured and uniform way of
                  handling system-level and application-level errors.
                – Exception handling is the process of providing an alternative
                  path to be executed when an application is not able to execute
                  in the desired.
                – In addition to handling pre-defined exceptions, users can
                  create their own exceptions by deriving an exception class
                  from the ApplicationException class.



    Ver. 1.0                        Session 14                            Slide 29 of 30
Object-Oriented Programming Using C#
Summary (Contd.)


               – You can only throw an object if the types of objects either
                 directly or indirectly derives from System.Exception.
               – You can use the throw statement to raise your own
                 exceptions.
               – A thread is defined as the path of execution of a program. It is
                 a sequence of instructions that is executed to define a unique
                 flow of control.
               – A program that creates two or more threads is called a
                 multithreaded program.
               – The System.Threading class is used to construct and
                 access individual threads in a multithreaded application.
               – The various states in the life cycle of a thread are:
                      Unstarted state
                      Runnable state
                      Not Runnable state
                      Dead state

    Ver. 1.0                       Session 14                            Slide 30 of 30

More Related Content

PPS
07 iec t1_s1_oo_ps_session_10
PPS
11 iec t1_s1_oo_ps_session_16
PPS
06 iec t1_s1_oo_ps_session_08
PPS
12 iec t1_s1_oo_ps_session_17
PPS
13 iec t1_s1_oo_ps_session_19
PPS
04 iec t1_s1_oo_ps_session_05
PPS
05 iec t1_s1_oo_ps_session_07
PPS
01 iec t1_s1_oo_ps_session_01
07 iec t1_s1_oo_ps_session_10
11 iec t1_s1_oo_ps_session_16
06 iec t1_s1_oo_ps_session_08
12 iec t1_s1_oo_ps_session_17
13 iec t1_s1_oo_ps_session_19
04 iec t1_s1_oo_ps_session_05
05 iec t1_s1_oo_ps_session_07
01 iec t1_s1_oo_ps_session_01

What's hot (20)

PPS
08 iec t1_s1_oo_ps_session_11
PPS
09 iec t1_s1_oo_ps_session_13
PPS
03 iec t1_s1_oo_ps_session_04
PPS
01 gui 01
PPS
02 iec t1_s1_plt_session_02
PPS
Java session02
PPS
Java session01
PPS
Java session11
PPS
Dacj 1-3 c
PDF
Oop02 6
PDF
Fudcon D programming
PPS
Dacj 1-2 a
PPS
03 iec t1_s1_plt_session_03
PDF
Oop04 6
PDF
Write native iPhone applications using Eclipse CDT
PPT
4.class diagramsusinguml
 
PPTX
Evolution of Patterns
PDF
Win32/Flamer: Reverse Engineering and Framework Reconstruction
PPS
01 iec t1_s1_plt_session_01
08 iec t1_s1_oo_ps_session_11
09 iec t1_s1_oo_ps_session_13
03 iec t1_s1_oo_ps_session_04
01 gui 01
02 iec t1_s1_plt_session_02
Java session02
Java session01
Java session11
Dacj 1-3 c
Oop02 6
Fudcon D programming
Dacj 1-2 a
03 iec t1_s1_plt_session_03
Oop04 6
Write native iPhone applications using Eclipse CDT
4.class diagramsusinguml
 
Evolution of Patterns
Win32/Flamer: Reverse Engineering and Framework Reconstruction
01 iec t1_s1_plt_session_01
Ad

Viewers also liked (7)

PPS
02 iec t1_s1_oo_ps_session_02
PPS
02 iec t1_s1_oo_ps_session_02
PDF
C# Advanced L04-Threading
PPT
Threads c sharp
PPT
Intro To .Net Threads
PPTX
Threading in C#
PDF
Threading in c#
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
C# Advanced L04-Threading
Threads c sharp
Intro To .Net Threads
Threading in C#
Threading in c#
Ad

Similar to 10 iec t1_s1_oo_ps_session_14 (20)

PPTX
Presentation1
PPTX
6-Error Handling.pptx
PPTX
Exception Handling in C#
PDF
Programming in Java Unit 1 lesson Notes for Java
PPTX
Module 4-packages and exceptions in java.pptx
PPS
Aae oop xp_13
PPTX
Exception handling, Stream Classes, Multithread Programming
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
PPTX
presentationon exception handling in java.pptx
PPTX
Exception handling in ASP .NET
DOCX
25 java tough interview questions
PPT
Introduction of exception in vb.net
PPTX
Unit2_2.pptx Chapter 2 Introduction to C#
PPTX
Lecture 1 Try Throw Catch.pptx
PPTX
JAVA Presenttation topics Programs.pptx
PPTX
1.4 core programming [understand error handling]
PDF
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
PPTX
Templates and Exception Handling in C++
PPTX
Lecture 3.1.1 Try Throw Catch.pptx
PPS
09 gui 13
Presentation1
6-Error Handling.pptx
Exception Handling in C#
Programming in Java Unit 1 lesson Notes for Java
Module 4-packages and exceptions in java.pptx
Aae oop xp_13
Exception handling, Stream Classes, Multithread Programming
JAVA PROGRAMMING- Exception handling - Multithreading
presentationon exception handling in java.pptx
Exception handling in ASP .NET
25 java tough interview questions
Introduction of exception in vb.net
Unit2_2.pptx Chapter 2 Introduction to C#
Lecture 1 Try Throw Catch.pptx
JAVA Presenttation topics Programs.pptx
1.4 core programming [understand error handling]
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
Templates and Exception Handling in C++
Lecture 3.1.1 Try Throw Catch.pptx
09 gui 13

More from Niit Care (20)

PPS
Ajs 1 b
PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 4 1-a
PPS
Dacj 1-2 b
PPS
Dacj 1-3 b
Ajs 1 b
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 4 1-a
Dacj 1-2 b
Dacj 1-3 b

Recently uploaded (20)

PDF
STKI Israel Market Study 2025 version august
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Five Habits of High-Impact Board Members
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PPT
Geologic Time for studying geology for geologist
PPTX
Modernising the Digital Integration Hub
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
The various Industrial Revolutions .pptx
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPT
What is a Computer? Input Devices /output devices
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Microsoft Excel 365/2024 Beginner's training
STKI Israel Market Study 2025 version august
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Five Habits of High-Impact Board Members
Benefits of Physical activity for teenagers.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
1 - Historical Antecedents, Social Consideration.pdf
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Geologic Time for studying geology for geologist
Modernising the Digital Integration Hub
sbt 2.0: go big (Scala Days 2025 edition)
Enhancing emotion recognition model for a student engagement use case through...
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Getting started with AI Agents and Multi-Agent Systems
The various Industrial Revolutions .pptx
A review of recent deep learning applications in wood surface defect identifi...
What is a Computer? Input Devices /output devices
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Microsoft Excel 365/2024 Beginner's training

10 iec t1_s1_oo_ps_session_14

  • 1. Object-Oriented Programming Using C# Objectives In this session, you will learn to: Handle exceptions Implement the user-defined exceptions Implement threads Define the life cycle of a thread Ver. 1.0 Session 14 Slide 1 of 30
  • 2. Object-Oriented Programming Using C# Handling Exceptions In exception handling, the application is divided into blocks of code. A block that shows the probability of raising an error contains one or more exception handlers. The exception handlers follow a control structure and a uniform way of handling the system level and application level errors. The blocks for exception-handling can be implemented using the following keywords: try catch finally Let us look at each of these keywords in detail. Ver. 1.0 Session 14 Slide 2 of 30
  • 3. Object-Oriented Programming Using C# Handling Exceptions (Contd.) The try block: The try block guards statements that may throw an exception. Following is the syntax of try block statement: try { //statements that may cause an exception } The try block governs statements that are enclosed within it and defines the scope of the exception-handlers associated with it. A try block must have at least one catch block. Ver. 1.0 Session 14 Slide 3 of 30
  • 4. Object-Oriented Programming Using C# Handling Exceptions (Contd.) The catch block: The catch statement of the catch block takes an object of the exception class as a parameter, which refers to the raised exception. You can associate an exception-handler with the try block by providing one or more catch handlers, immediately after the try block: try { //statements that may cause an exception } catch (…) { //error handling code } Ver. 1.0 Session 14 Slide 4 of 30
  • 5. Object-Oriented Programming Using C# Handling Exceptions (Contd.) The finally block: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown: try { //statements that may cause an exception } catch (…) { //error handling code } finally { //statements to be executed } Ver. 1.0 Session 14 Slide 5 of 30
  • 6. Object-Oriented Programming Using C# Demo: Handling Exception for Arrays Beyond Limit Problem Statement: David is working on a project where he is calculating the sum of values in an integer array. David needs to handle the exceptions, which can occur while he is working with the arrays. If any exceptional condition is reached when David is executing the application, the application needs to display an exception message. Help David to handle the exceptions. Ver. 1.0 Session 14 Slide 6 of 30
  • 7. Object-Oriented Programming Using C# Demo: Handling Exception for Arrays Beyond Limit (Contd.) Solution: To develop a console-based application, David need to perform the following tasks: 1. Create a console-based application. 2. Build and execute an application. Ver. 1.0 Session 14 Slide 7 of 30
  • 8. Object-Oriented Programming Using C# Implementing the User-Defined Exceptions In C#, you can create your own exception class. Such kinds of exceptions are known as user-defined exceptions. The Exception class is the base class for all the exceptions in C#. The user-defined exception classes must follow the hierarchy of either the exception class or of one of the standard inherited classes. Ver. 1.0 Session 14 Slide 8 of 30
  • 9. Object-Oriented Programming Using C# Implementing the User-Defined Exceptions (Contd.) User-defined exception classes are derived from the ApplicationException class. To implement user-defined exceptions, you need to: – Raise your exception: You can use the throw statement to raise your own exceptions. – Throw an object: You can throw an object if the object is either directly or indirectly derived from System.Exception. You can use a throw statement in the catch block to throw the present object, as shown in the following code: catch(Exception caught) { . . . throw caught } Ver. 1.0 Session 14 Slide 9 of 30
  • 10. Object-Oriented Programming Using C# Implementing Threads A thread is defined as the execution path of a program. You can define a unique flow of a control in a program, using a thread. Threads are used to run applications that perform large and complex computations. A process that is executed using one thread is known as a single-threaded process, where the process is a running instance of a program. Single-threaded application can perform only one task at a time. You have to wait for one task to complete before another task can start. Ver. 1.0 Session 14 Slide 10 of 30
  • 11. Object-Oriented Programming Using C# Implementing Threads (Contd.) The following figure shows a single-threaded process. • To execute more than one task at a time, you can create multiple threads in a program. • A process that creates two or more threads is called a multithreaded process. Ver. 1.0 Session 14 Slide 11 of 30
  • 12. Object-Oriented Programming Using C# Implementing Threads (Contd.) The following figure shows a multithreaded process. Ver. 1.0 Session 14 Slide 12 of 30
  • 13. Object-Oriented Programming Using C# The Thread Model in C# In single-threaded systems, an approach called event loop with polling is used. Polling is the process in which a single event is executed at a time. In the event loop with polling approach, a single thread runs in an infinite loop till its operation is completed. In a single-threaded application if the thread is suspended from execution because it is waiting for a system resource, the entire program stops executing. In multithreading, the time for which a thread waits for the CPU time can be utilized to perform another task. Ver. 1.0 Session 14 Slide 13 of 30
  • 14. Object-Oriented Programming Using C# The Thread Model in C#(Contd.) • In C#, you will use the Thread class to work with threads. • The System.Threading.Thread class is used to construct and access individual threads in a multithreaded application. Ver. 1.0 Session 14 Slide 14 of 30
  • 15. Object-Oriented Programming Using C# The Main Thread • The main thread is created automatically on the start up of a C# program execution. • The threads which are created exclusively using the Thread class are called as child threads, where the main thread is called a parent thread or a primary thread. • You can access a thread using the CurrentThread property of the Thread class. Ver. 1.0 Session 14 Slide 15 of 30
  • 16. Object-Oriented Programming Using C# Working with Threads • In C#, you create a thread by creating an object of type Thread, giving its constructor a ThreadStart reference, and calling the new thread’s Start() method. • The new thread starts executing asynchronously with an invocation of the thread’s method. • There are various methods available with the Thread class. Using these methods, you can control the execution of threads. Few of these methods are: – Start(): Starts a thread. – Sleep(): Makes the thread to pause for a period of time. – Abort(): Terminates the thread. – Suspend(): Suspends a thread. If the thread is already suspended it has no effect. – Resume(): Resumes the suspended thread. Ver. 1.0 Session 14 Slide 16 of 30
  • 17. Object-Oriented Programming Using C# Creating Threads • You can create threads by extending the Thread class. • The extended thread class calls the Start() method to begin the child thread execution. Following is an example of creating threads: ThreadStart ChildRef = new ThreadStart(ChildThreadCall); Thread ChildThread = new Thread(ChildRef); ChildThread.Start(); Ver. 1.0 Session 14 Slide 17 of 30
  • 18. Object-Oriented Programming Using C# Managing Threads • There are many tasks you might need to perform to manage the activity or life of a thread. • You can manage all these tasks by using the various thread methods available with the Thread class. • The static Thread.Sleep() method calls the static CurrentThread method, which then pauses that thread for the specified amount of time. Ver. 1.0 Session 14 Slide 18 of 30
  • 19. Object-Oriented Programming Using C# Destroying Threads • If the thread is required to be destroyed, the Thread.Abort() method will allow you to accomplish the task. • The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught. • If the finally block is present in the method, the runtime will send the control to it. Ver. 1.0 Session 14 Slide 19 of 30
  • 20. Object-Oriented Programming Using C# Thread Life Cycle • The lifecycle of a thread starts when an object of the System.Threading.Thread class is created. The life cycle of the thread ends with task execution. • There are various states in the life cycle of a thread. These states are: The Unstarted state The Runnable state The Not Runnable state The Dead state Let us understand the life cycle of the thread with the help of the following figure. Ver. 1.0 Session 14 Slide 20 of 30
  • 21. Object-Oriented Programming Using C# Start() Started Work Completed Stopped Ver. 1.0 Session 14 Slide 21 of 30
  • 22. Object-Oriented Programming Using C# Start() Started Suspend() Suspended Resume() Ver. 1.0 Session 14 Slide 22 of 30
  • 23. Object-Oriented Programming Using C# Start() Started Sleep() Wait/Join Sleep Interrupt() Time Expires Ver. 1.0 Session 14 Slide 23 of 30
  • 24. Object-Oriented Programming Using C# Start() Started Thread Responds to Abort() Stop Request Stop Request Stopped Ver. 1.0 Session 14 Slide 24 of 30
  • 25. Object-Oriented Programming Using C# The Unstarted State • When an instance of the Thread class is created, the thread enters the unstarted state. • A new thread is an empty object of the Thread class, and no system resources such as memory are allocated to it. Ver. 1.0 Session 14 Slide 25 of 30
  • 26. Object-Oriented Programming Using C# The Runnable State • The thread remains in the unstarted state until the program calls the Start() method of the Thread class, which places the thread in the runnable state and immediately returns control to the calling thread. • This state is also called as the ready or started state. • The newly started thread and any other threads in the program execute concurrently. Ver. 1.0 Session 14 Slide 26 of 30
  • 27. Object-Oriented Programming Using C# The Not Runnable State A thread is not in the runnable state if it is: Sleeping Waiting Blocked Ver. 1.0 Session 14 Slide 27 of 30
  • 28. Object-Oriented Programming Using C# The Dead State • A running thread enters the dead state when the statements of the threads method are complete. This state is also called the terminated state. • A program can force a thread into the dead state by calling the Abort() method of the Thread class on the appropriate thread object. Ver. 1.0 Session 14 Slide 28 of 30
  • 29. Object-Oriented Programming Using C# Summary In this session, you learned that: Exception handling is implemented using the following keywords: • try • catch • finally – Exception-handling provides a structured and uniform way of handling system-level and application-level errors. – Exception handling is the process of providing an alternative path to be executed when an application is not able to execute in the desired. – In addition to handling pre-defined exceptions, users can create their own exceptions by deriving an exception class from the ApplicationException class. Ver. 1.0 Session 14 Slide 29 of 30
  • 30. Object-Oriented Programming Using C# Summary (Contd.) – You can only throw an object if the types of objects either directly or indirectly derives from System.Exception. – You can use the throw statement to raise your own exceptions. – A thread is defined as the path of execution of a program. It is a sequence of instructions that is executed to define a unique flow of control. – A program that creates two or more threads is called a multithreaded program. – The System.Threading class is used to construct and access individual threads in a multithreaded application. – The various states in the life cycle of a thread are: Unstarted state Runnable state Not Runnable state Dead state Ver. 1.0 Session 14 Slide 30 of 30

Editor's Notes

  • #2: 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.
  • #3: 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.
  • #4: 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.
  • #5: 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.
  • #6: 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.
  • #7: 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.
  • #8: 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.
  • #9: 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.
  • #10: 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.
  • #11: 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.
  • #12: 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.
  • #17: 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.
  • #18: 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.
  • #26: 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.
  • #27: 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.
  • #28: 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.
  • #29: 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.
  • #30: You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  • #31: You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.