Anda di halaman 1dari 71

Session 3

Module 5: COM and Interoperability Module 4: Multithreading in C#

Module 3 - Review (1)


Data is stored permanently in the form of files.  A stream is a flow of data.  A stream helps data traveling from one source point to another. These source points are file, memory, network, web, string and so on.  C# provides the System.IO namespace for file and stream handling.

COM and Multithreading / Session3 / 2 of 71

Module 3 - Review (2)


 

The BinaryReader class is used to read binary data from a stream. The BinaryWriter class is used for writing binary data from a C# variable to a specified stream. The Stream class is an abstract class from which different stream processing classes are being derived. The TextReader is the abstract base class for the StreamReader and StringReader classes.

COM and Multithreading / Session3 / 3 of 71

Module 3 - Review (3)




 

The TextWriter is an abstract base class for classes that can be used for writing sequential characters. The File class provide static methods for the creation, copying, deletion, moving and opening, accessing information of files. The FileInfo class is statefull and can be used to perform identical operations as the File class. The Directory class contains static methods for creating, moving and enumerating through the directories and subdirectories.

COM and Multithreading / Session3 / 4 of 71

Module 4 - Objectives
Introduces concept of threads  Explains how to create threads using C#  Discusses the Thread class, its properties, methods and lifecycle.  Explains about thread priorities and thread safety.


COM and Multithreading / Session3 / 5 of 71

Multithreading concept


An application consists of one or more processes. Every process typically has a minimum of one thread. A thread is the smallest unit of execution and has a set of executable instructions. The process of performing tasks and executing simultaneously with other threads is known as multithreading. The first thread in an application is born when the application process starts executing, is known as primary thread.
COM and Multithreading / Session3 / 6 of 71

Implementation of multithreading in .NET


.NET framework supports a multithreaded operation right from its beginning to end.  Multithreading can be implemented in .NET by:

 

Starting the threads with ThreadStart delegates. Using the ThreadPool class either directly or indirectly using asynchronous methods.

COM and Multithreading / Session3 / 7 of 71

Situations where Threads can be used

COM and Multithreading / Session3 / 8 of 71

Situations where not to use Threads

COM and Multithreading / Session3 / 9 of 71

Features

COM and Multithreading / Session3 / 10 of 71

Benefit

COM and Multithreading / Session3 / 11 of 71

Threading namespace
Multithread programming is possible in .NET with the classes and interfaces provided in the System.Threading namespace.  The classes and interfaces in the namespace help in:


COM and Multithreading / Session3 / 12 of 71

Classes in System.Threading namespace




The commonly used classes of this namespace are list in the table

COM and Multithreading / Session3 / 13 of 71

Steps to create threads (1)

COM and Multithreading / Session3 / 14 of 71

Steps to create threads (2)

COM and Multithreading / Session3 / 15 of 71

Steps to create threads (3)

COM and Multithreading / Session3 / 16 of 71

Steps to create threads (4)

COM and Multithreading / Session3 / 17 of 71

Steps to create threads (5)

COM and Multithreading / Session3 / 18 of 71

Steps to create threads (6)

COM and Multithreading / Session3 / 19 of 71

Thread class
The Thread class belongs to the System.Threading namespace.  The Thread class can be used to create new threads, control and manipulate them.  The Start() method in the Thread class starts the execution of the thread.


COM and Multithreading / Session3 / 20 of 71

ThreadStart delegate (1)


.NET Framework carries out multithreading operations through the use of delegates.  The ThreadStart delegate represents the method that will be used to execute on threads.  When a thread is being created, an instance of ThreadStart is passed as an argument to the constructor of the Thread class.  When the start() method is called on the thread object, the object will invoke the delegate in its own thread of execution.

COM and Multithreading / Session3 / 21 of 71

ThreadStart delegate (2)

COM and Multithreading / Session3 / 22 of 71

Example

COM and Multithreading / Session3 / 23 of 71

Properties of Thread class




The properties of Thread class control and manage the basic functionality of a thread class.

COM and Multithreading / Session3 / 24 of 71

Example

COM and Multithreading / Session3 / 25 of 71

Methods of Thread class




Some of the common methods are listed in the table:

COM and Multithreading / Session3 / 26 of 71

Example

COM and Multithreading / Session3 / 27 of 71

Thread states
 

A thread is always in some state or the other right from the time of its creation until its termination. The ThreadState property of Thread class defines the state of a thread during its execution and provides information about the thread status.

COM and Multithreading / Session3 / 28 of 71

Example

COM and Multithreading / Session3 / 29 of 71

Lifecycle of a Thread

COM and Multithreading / Session3 / 30 of 71

Thread priorities
The Priority property of Thread class determines the execution schedule for a thread as compared to other active threads in the same process by assigning priorities.  Initially, the system handles all the threads with the same priority.  Every thread has a priority that ranges between ThreadPriority.Lowest to ThreadPriority.Highest

COM and Multithreading / Session3 / 31 of 71

Example

COM and Multithreading / Session3 / 32 of 71

Need for thread safety


Many threads can interoperate on the same data structures at the same time. In such cases, it's quite possible that the fields and data may become inconsistent.  Thread-safety is attained by using the concept of locking, and by reducing the possibilities of interaction between threads.  A method which is thread-safe is known as reentrant.

COM and Multithreading / Session3 / 33 of 71

Implementing Thread Safety




An object can be made thread-safe by implementing a minimum of three concepts:

COM and Multithreading / Session3 / 34 of 71

Synchronize Critical Sections




The standard way of making objects thread-safe is to identify and synchronize their critical sections. A part of code in a program is known as critical section if it is accessed by multiple threads at the same time.

COM and Multithreading / Session3 / 35 of 71

Make the object immutable




When the state of an object cannot be changed after it has been created, it's said to be immutable. This is achieved by not allowing any thread to modify its state. It can be done by separating the critical sections that read the instance variables from those that write to instance variables.

COM and Multithreading / Session3 / 36 of 71

Using a Thread-safe Wrapper




The third way to make an object thread-safe is to write wrapper class for the object that should be thread-safe rather than making the object itself thread-safe. This method is useful when the classes in a third-party library are not designed for thread safety.

COM and Multithreading / Session3 / 37 of 71

Concept of locking


Locking is a concept which allows only one thread to exclusively access particular sections of code at a time. By using the lock keyword, a block of statements can be made as a synchronized section.

COM and Multithreading / Session3 / 38 of 71

Thread synchronization


An object needs to be synchronized when multiple threads make calls to its properties and methods. Using the Monitor class of C# is a primary mechanism to implement this. The commonly used methods of this class as listed below:

COM and Multithreading / Session3 / 39 of 71

Thread pooling with ThreadPool class




It's cumbersome as well as unsafe for a developer to do all the managing and maintaining of threads. C# provides the ThreadPool class which gives an application a ready set of worker threads in a pool, from which one or more can be chosen for any short tasks. Using the ThreadPool class, thread management is done by the system, the developer can focus on actual application development instead of spending all the time on managing threads.
COM and Multithreading / Session3 / 40 of 71

Module 4 - Summary (1)


A thread is the smallest unit of execution and has a set of executable instructions.  .NET framework supports by

 

Starting the threads with ThreadStart delegates. Using the ThreadPool class either directly or indirectly using asynchronous methods.

Classes and interfaces for multithreading provided in the System.Threading namespace.  .NET Framework carries out multithreading operations through the use of delegates.

COM and Multithreading / Session3 / 41 of 71

Module 4 - Summary (2)


A thread is always in some state or the other right from the time of its creation until its termination.  The ThreadState property of Thread class defines the state of a thread during its execution.  The Priority property of Thread class determines priority of the thread.  Initially, the system handles all the threads with the same priority.

COM and Multithreading / Session3 / 42 of 71

Module 4 - Summary (3)




Thread-safety is attained by using the concept of locking, and by reducing the possibilities of interaction between threads. An object can be made thread-safe by implementing a minimum of three concepts
  

Synchronize Critical Sections Make the object immutable Using a Thread-safe Wrapper

Locking is a concept which allows only one thread to exclusively access particular sections of code at a time. Using the ThreadPool class, thread management is done by the system.
COM and Multithreading / Session3 / 43 of 71

Module 5 - Objectives
Introduces the concept of Component Object Model and transition from COM to .NET  Explains how to use COM components from .NET  Discusses the assembly register tool and COM Callable Wrapper  Explain about platform invocation


COM and Multithreading / Session3 / 44 of 71

History of Component Object Model COM is a software technology developed by Microsoft in 1993 and implemented after 1997.  COM comprises of technologies like OLE, OLE Automation, ActiveX, COM+, and DCOM.  The importance of COM is to use objects across different environments and machines.  COM permits reuse of objects without having any knowledge of their interior execution.

COM and Multithreading / Session3 / 45 of 71

COM (1)
COM is a methodology in which binary code is shared between various applications and languages through interfaces.  An object's Globally Unique Identifier (GUID) is a 128-bit number that be referenced when COM or DCOM object is registered within the window registry.  Language independence is another characteristic of COM.

COM and Multithreading / Session3 / 46 of 71

COM (2)


During runtime, COM clients and COM objects have the same layout and behavior irrespective of the language used to produce the component. Language independence of COM allow building the systems with different components of various languages.

COM and Multithreading / Session3 / 47 of 71

Characteristics of COM
It separates large systems into subsystems in the design phase.  It helps in implementing each subsystem with a component that is created using COMcapable tool.


COM and Multithreading / Session3 / 48 of 71

Evolution of .NET components


.NET is a component technology like COM and helps developers to build components. It eventually take-over COM as its successor.  Though COM has cross-language code reuse capability, it does not provide the facilities like cross language inheritance.  COM will not be eliminated with the advent of .NET, a lot of business logic still resides in COM.

COM and Multithreading / Session3 / 49 of 71

Need for Interoperability


The internal structure of COM components is different from .NET components -> they're not compatible.  The legacy components are exploited by using the feature of interoperability.  Interoperability can be done by two ways:


COM and Multithreading / Session3 / 50 of 71

Situations for Interoperability


If the developer is new to .NET, the core development in COM will come in handy.  Some components cannot be moved to .NET because of implementation problems.  Some third-party components do not have source code.


COM and Multithreading / Session3 / 51 of 71

Implementation of COM Interoperability




COM interoperability can be implemented by two ways

COM and Multithreading / Session3 / 52 of 71

Using COM components from .NET




The steps to use the COM components directly from .NET are:
1.

2. 3. 4.

Add a reference to the project using Project -> Add Reference Click the COM tab in the Add Reference dialog box Select the component from the list Click OK to create RCWs for the selected type library object

.NET will create a DLL in the project's bin folder with same name as COM component.
COM and Multithreading / Session3 / 53 of 71

Type library import tool (tlbimp)


Type Library Importer converts the type definitions present in a COM type library into definitions in a common language runtime assembly.  Type library file is created after the project is compiled.  The output of tlbimp.exe is a binary file that is an assembly containing metadata for all the types in the original type library.

COM and Multithreading / Session3 / 54 of 71

Runtime Callable Wrapper (RCW)




The managed code communicates with COM objects through a proxy RCW. The primary function of RCW is to connect the calls between a .NET client and a COM object. RCW marshals the calls between managed and unmanaged codes by hiding the differences between them.
COM and Multithreading / Session3 / 55 of 71

Runtime Callable Wrapper (RCW)




The RCW can be created by using the utility tlbimp or by using Visual Studio. The TypeLibConverter class is used to generate RCW. After creating RCW, .NET client should import namespace so that it can use native calls to call the RCW object.

COM and Multithreading / Session3 / 56 of 71

Working of RCW
Runtime environment creates one RCW for every COM object. A single RCW per process is maintained for each object.  The runtime environment creates both the called COM object and its wrapper. These created with the help of the metadata derived from the type library.  Every RCW maintains a cache of all the interface pointers on its wrapped COM object.

COM and Multithreading / Session3 / 57 of 71

Interaction between COM component and .NET clients using RCW


  

Using RCW, the .NET client considers a COM object as a .NET object. .NET client calls to functions are moved to RCW. RCW internally calls the native COM function CoCreateInstance to create a wrapped COM object and .NET client can accesses that object like native object calls. The RCW converts .NET data type into the COM compatible data type. Ex: .NET data type, string into COM compatible data type BSTR.

COM and Multithreading / Session3 / 58 of 71

COM Callable Wrapper (CCW)


When a COM client communicates with a .NET object, the runtime creates the managed object and COM callable wrapper for the object.  CCW wraps the .NET components and interacts with the COM clients.  Microsoft .NET objects cannot be referenced directly. Therefore, COM clients marshal with the managed objects by using CCW as a proxy.

COM and Multithreading / Session3 / 59 of 71

Working of CCW


The runtime framework creates one CCW for every managed object. COM client hold a reference to the CCW by exposing the INew interface.

COM and Multithreading / Session3 / 60 of 71

Interfaces of COM callable wrapper




CCW also provides some interfaces for explicit implementation by classes and events in the managed environment:

COM and Multithreading / Session3 / 61 of 71

Assembly Registration Tool (regasm.exe)


  

Regasm.exe is a tool that develops a type library from a .NET assembly. Regasm registers type library and its COM classes in Windows registry. The main function of Regasm is to go through the metadata in the assembly and add the necessary entries to the registry. After the class is registered, the COM client can use it like any other COM class.


regasm <assemblyFile> [options]

COM and Multithreading / Session3 / 62 of 71

Using a .NET component from a COM client


 

To invoke .NET components in COM, a .NET assembly should be created. The steps to call .NET components in COM:  1. Create a type library


tblexp DotnetComponent.dll regasm /tlb:DotnetComponent.tlb DotnetComponent.dll gacutil /i DotnetComponent.dll


COM and Multithreading / Session3 / 63 of 71

2. Use the regasm to register




3. Install the .NET assembly




Type Library Exporter (tlbexp tool)


The two tools that are necessary for COM interoperability are Type library exporter and importer.  The main function of tlbexp utility is to generate a type library for a .NET framework assembly that is used by the COM components to interop with .NET components.


COM and Multithreading / Session3 / 64 of 71

Need of legacy code and unmanaged APIs

COM and Multithreading / Session3 / 65 of 71

Platform Invacation


Platform Invocation Services (PInvoke) is a technology that allows managed code to call unmanaged functions implemented in a DLL. PInvoke covers the differences between the managed and unmanaged codes by marshalling between CLR data types and native data types.

COM and Multithreading / Session3 / 66 of 71

Calling unmanaged code

COM and Multithreading / Session3 / 67 of 71

Working of Platform Invocation

COM and Multithreading / Session3 / 68 of 71

Module 5 - Summary (1)


COM comprises of technologies like OLE, OLE Automation, ActiveX, COM+, and DCOM.  The importance of COM is to use objects across different environments and machines.  .NET is a component technology like COM and helps developers to build components.  COM will not be eliminated with the advent of .NET, a lot of business logic still resides in COM.

COM and Multithreading / Session3 / 69 of 71

Module 5 - Summary (2)


The internal structure of COM components is different from .NET components -> they're not compatible.  Type tlbimp utility converts the type definitions present in a COM type library into a common language runtime assembly.  The managed code communicates with COM objects through a proxy known as Runtime Callable Wrapper (RCW).

COM and Multithreading / Session3 / 70 of 71

Module 5 - Summary (3)


The primary function of RCW is to connect the calls between a .NET client and a COM object.  Regasm.exe registers type library and its COM classes in Windows registry.  Platform Invocation Services (PInvoke) is a technology that allows managed code to call unmanaged functions implemented in a DLL.


COM and Multithreading / Session3 / 71 of 71

Anda mungkin juga menyukai