SlideShare a Scribd company logo
1 of 37
What is Thread
Thread vs Process
• A Process is inert. A process never
executes anything it is simply a
container for threads.
• Threads run in the context of a
process. Each process has at least
one thread.
• A thread represents a path of
execution that has its own call stack
and CPU state.
• Threads are confined to context of
the process that created them.
• A thread executes code and
manipulates data within its process
address space.
• If two or more threads run in the
context of a single process they
share a common address space.
• They can execute the same code
and manipulate the same data.
• Threads sharing a common process
can share kernel object handles
because the handles belong to the
process, not individual threads.
Starting a Process
• Every time a process starts, the system creates a primary thread.
• The thread begins execution with the C/C run-time library startup
code.
• The startup code calls your main or WinMain and execution continues
until the main function returns and the C/C library code calls
ExitProcess.
Scheduling Threads
• Windows 2000, NT and Win98 are preemptive multi-tasking systems. Each
task is scheduled to run for some brief time period before another task is given
control of CPU.
• Threads are the basic unit of scheduling on current Win32 platforms. A thread
may be in one of three possible states
o running
o blocked or suspended, using virtually no CPU cycles
o ready to run, using virtually no CPU cycles
• A running task is stopped by the scheduler if
• it is blocked waiting for some system event or resource
Con’t
• its time slice expires and is placed back on the queue of ready to run threads
• it is suspended by putting itself to sleep for some time
• it is suspended by some other thread
• it is suspended by the operating system while the OS takes care of some other
critical activity.
• Blocked threads become ready to run when an event or resource they wait on
becomes available.
• Suspended threads become ready to run when their sleep interval has expired or
suspend count is zero.
Benefits of using Threads
• Keeping user interfaces responsive even if required processing takes a long
time to complete.
• handle background tasks with one or more threads
• service the user interface with a dedicated thread
• Your program may need to respond to high priority events. In this case, the
design is easier to implement if you assign that event handler to a high
priority thread.
• Take advantage of multiple processors available for a computation.
• Avoid low CPU activity when a thread is blocked waiting for response from
a slow device or human by allowing other threads to continue.
Potential Problems with Threads
• Conflicting access to shared memory
• one thread begins an operation on shared memory, is suspended, and leaves
that memory region incompletely transformed
• a second thread is activated and accesses the shared memory in the
corrupted state, causing errors in its operation and potentially errors in the
operation of the suspended thread when it resumes
• Race Conditions occur when correct operation depends on the order of
completion of two or more independent activities
• the order of completion is not deterministic Starvation
• a high priority thread dominates CPU resources, preventing lower priority
threads from running often enough or at all.
Synchronization
• A program may need multiple threads to share some data.
• If access is not controlled to be sequential, then shared data may become
corrupted.
• One thread accesses the data, begins to modify the data, and then is put to sleep
because its time slice has expired. The problem arises when the data is in an
incomplete state of modification.
• Another thread awakes and accesses the data, that is only partially modified. The
result is very likely to be corrupt data.
• The process of making access serial is called serialization or synchronization.
Thread Synchronization
• Synchronizing threads means that every access to data shared between threads is protected so that when any
thread starts an operation on the shared data no other thread is allowed access until the first thread is done.
• The principle means of synchronizing access to shared data are
• Interlocked increments
• only for incrementing or decrementing integers
• Critical Sections
• Good only inside one process
• Mutexes
• Named mutexes can be shared by threads in different processes.
• Events
• Useful for synchronization as well as other event notifications.
Private Assembly Deployment
Shared Assembly Deployment
Late Binding
Types of Binding
• There are two kinds of Binding from C# to COM – Early Binding and Late Binding.
• Early Binding can be done by creating a runtime callable wrapper, which the C#
client can use for invoking COM objects. That’s what happens when you make a
reference in a C# client to a COM server.
• Late Binding can be done even without the creation of a runtime callable
wrapper. We will see how.
Late Binding
• Late Binding is done with the help of the C# Reflection APIs.
• The Type class and the Activator class of the C# Reflection API is used
for this purpose.
• The C# client only needs to know the server’s Program ID for runtime
invocation. The following code shows how to accomplish that.
Using C# Reflection for Late Binding
//Get IDispatch Interface from the COM Server. Here the Server’s Program ID is “Component.InsideDCOM”
Type objType = Type.GetTypeFromProgID(“Component.InsideDCOM”);
//Create an instance of the COM object from the type obtained
object objSum = Activator.CreateInstance(objType);
object c;
object[] myArgument = {100,200};
//Invoke a Method on the COM Server which implements IDispatch Interface and get the result
c = objType.InvokeMember("Sum", BindingFlags.InvokeMethod, null, objSum, myArgument);
//Print the result
Console.WriteLine(“Sum of 100 and 200 is “ + c);
Reflection
Reflection
• Reflection is the ability of a managed code to read its own metadata
for the purpose of finding assemblies, modules and type information
at runtime. The classes that give access to the metadata of a running
program are in System.Reflection.
• System.Reflection namespace defines the following types to analyze
the module's metadata of an assembly:
• Assembly, Module, Enum, ParameterInfo, MemberInfo, Type,
MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo.
Assembly Metadata
• An assembly is a logical DLL or EXE, which is described by manifest,
the detailed description (metadata) of an assembly. The .NET
compiler produces a portable executable PE file for CLR with the
extensions of .exe or .dll. This PE file is mainly comprised of metadata
and IL (Intermediate Language).
• When a compiler builds a binary (dll or exe), the code gets complied into
the Intermediate Language (IL) and then packaged in an assembly. An
assembly contains metadata which has the information about the type of
classes and their definitions including their members, fields, constructors,
properties, methods, function etc.
• By using refection, an assembly can be inspected.
Type _type = Type.GetType("ReflectionConcept.Employee");
Or
Type _type = typeof(Employee);
Uses of Reflections
• Refection is heavily used by developer IDEs or UI designers such as
Visual Studio. The Property window of a .NET application uses
refection to show list of properties.
• Late binding can be achieved by using refection. Meaning, reflection
gives developers a way to use code that is not available at compile
time. By using refection, the new instances of types can be created
dynamically, which don’t have information at compile time.
• In a Web application, whenever developers drags and drop a control
from Toolbox to the Form, the code is written for you. Here reflection
plays the role and used to read control properties and events. Thease
are listed in the Properties window.
• It allows view attribute information at runtime.
• It allows examining various types in an assembly and instantiate these
types.
• It allows late binding to methods and properties
• It allows creating new types at runtime and then performs some tasks
using those types.

More Related Content

Similar to Scheduling Thread

Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentationchnrketan
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptxHamzaxTv
 
chapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptchapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptaakarshsiwani1
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threadsVaibhav Khanna
 
Unit 2 part 2(Process)
Unit 2 part 2(Process)Unit 2 part 2(Process)
Unit 2 part 2(Process)WajeehaBaig
 
Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Hardeep Kaur
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaLuis Goldster
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHarry Potter
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaYoung Alista
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaTony Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaFraboni Ec
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaHoang Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaJames Wong
 

Similar to Scheduling Thread (20)

Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptx
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
chapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptchapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.ppt
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Unit 2 part 2(Process)
Unit 2 part 2(Process)Unit 2 part 2(Process)
Unit 2 part 2(Process)
 
Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Scheduling Thread

  • 1.
  • 3. Thread vs Process • A Process is inert. A process never executes anything it is simply a container for threads. • Threads run in the context of a process. Each process has at least one thread. • A thread represents a path of execution that has its own call stack and CPU state. • Threads are confined to context of the process that created them. • A thread executes code and manipulates data within its process address space. • If two or more threads run in the context of a single process they share a common address space. • They can execute the same code and manipulate the same data. • Threads sharing a common process can share kernel object handles because the handles belong to the process, not individual threads.
  • 4. Starting a Process • Every time a process starts, the system creates a primary thread. • The thread begins execution with the C/C run-time library startup code. • The startup code calls your main or WinMain and execution continues until the main function returns and the C/C library code calls ExitProcess.
  • 5. Scheduling Threads • Windows 2000, NT and Win98 are preemptive multi-tasking systems. Each task is scheduled to run for some brief time period before another task is given control of CPU. • Threads are the basic unit of scheduling on current Win32 platforms. A thread may be in one of three possible states o running o blocked or suspended, using virtually no CPU cycles o ready to run, using virtually no CPU cycles • A running task is stopped by the scheduler if • it is blocked waiting for some system event or resource
  • 6. Con’t • its time slice expires and is placed back on the queue of ready to run threads • it is suspended by putting itself to sleep for some time • it is suspended by some other thread • it is suspended by the operating system while the OS takes care of some other critical activity. • Blocked threads become ready to run when an event or resource they wait on becomes available. • Suspended threads become ready to run when their sleep interval has expired or suspend count is zero.
  • 7. Benefits of using Threads • Keeping user interfaces responsive even if required processing takes a long time to complete. • handle background tasks with one or more threads • service the user interface with a dedicated thread • Your program may need to respond to high priority events. In this case, the design is easier to implement if you assign that event handler to a high priority thread. • Take advantage of multiple processors available for a computation. • Avoid low CPU activity when a thread is blocked waiting for response from a slow device or human by allowing other threads to continue.
  • 8. Potential Problems with Threads • Conflicting access to shared memory • one thread begins an operation on shared memory, is suspended, and leaves that memory region incompletely transformed • a second thread is activated and accesses the shared memory in the corrupted state, causing errors in its operation and potentially errors in the operation of the suspended thread when it resumes • Race Conditions occur when correct operation depends on the order of completion of two or more independent activities • the order of completion is not deterministic Starvation • a high priority thread dominates CPU resources, preventing lower priority threads from running often enough or at all.
  • 9. Synchronization • A program may need multiple threads to share some data. • If access is not controlled to be sequential, then shared data may become corrupted. • One thread accesses the data, begins to modify the data, and then is put to sleep because its time slice has expired. The problem arises when the data is in an incomplete state of modification. • Another thread awakes and accesses the data, that is only partially modified. The result is very likely to be corrupt data. • The process of making access serial is called serialization or synchronization.
  • 10. Thread Synchronization • Synchronizing threads means that every access to data shared between threads is protected so that when any thread starts an operation on the shared data no other thread is allowed access until the first thread is done. • The principle means of synchronizing access to shared data are • Interlocked increments • only for incrementing or decrementing integers • Critical Sections • Good only inside one process • Mutexes • Named mutexes can be shared by threads in different processes. • Events • Useful for synchronization as well as other event notifications.
  • 11. Private Assembly Deployment Shared Assembly Deployment
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 28. Types of Binding • There are two kinds of Binding from C# to COM – Early Binding and Late Binding. • Early Binding can be done by creating a runtime callable wrapper, which the C# client can use for invoking COM objects. That’s what happens when you make a reference in a C# client to a COM server. • Late Binding can be done even without the creation of a runtime callable wrapper. We will see how.
  • 29. Late Binding • Late Binding is done with the help of the C# Reflection APIs. • The Type class and the Activator class of the C# Reflection API is used for this purpose. • The C# client only needs to know the server’s Program ID for runtime invocation. The following code shows how to accomplish that.
  • 30. Using C# Reflection for Late Binding //Get IDispatch Interface from the COM Server. Here the Server’s Program ID is “Component.InsideDCOM” Type objType = Type.GetTypeFromProgID(“Component.InsideDCOM”); //Create an instance of the COM object from the type obtained object objSum = Activator.CreateInstance(objType); object c; object[] myArgument = {100,200}; //Invoke a Method on the COM Server which implements IDispatch Interface and get the result c = objType.InvokeMember("Sum", BindingFlags.InvokeMethod, null, objSum, myArgument); //Print the result Console.WriteLine(“Sum of 100 and 200 is “ + c);
  • 32. Reflection • Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. The classes that give access to the metadata of a running program are in System.Reflection. • System.Reflection namespace defines the following types to analyze the module's metadata of an assembly: • Assembly, Module, Enum, ParameterInfo, MemberInfo, Type, MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo.
  • 33. Assembly Metadata • An assembly is a logical DLL or EXE, which is described by manifest, the detailed description (metadata) of an assembly. The .NET compiler produces a portable executable PE file for CLR with the extensions of .exe or .dll. This PE file is mainly comprised of metadata and IL (Intermediate Language).
  • 34. • When a compiler builds a binary (dll or exe), the code gets complied into the Intermediate Language (IL) and then packaged in an assembly. An assembly contains metadata which has the information about the type of classes and their definitions including their members, fields, constructors, properties, methods, function etc. • By using refection, an assembly can be inspected. Type _type = Type.GetType("ReflectionConcept.Employee"); Or Type _type = typeof(Employee);
  • 35.
  • 36. Uses of Reflections • Refection is heavily used by developer IDEs or UI designers such as Visual Studio. The Property window of a .NET application uses refection to show list of properties. • Late binding can be achieved by using refection. Meaning, reflection gives developers a way to use code that is not available at compile time. By using refection, the new instances of types can be created dynamically, which don’t have information at compile time. • In a Web application, whenever developers drags and drop a control from Toolbox to the Form, the code is written for you. Here reflection plays the role and used to read control properties and events. Thease are listed in the Properties window.
  • 37. • It allows view attribute information at runtime. • It allows examining various types in an assembly and instantiate these types. • It allows late binding to methods and properties • It allows creating new types at runtime and then performs some tasks using those types.