session3 module4-5 com multi threading

Upload: hideip

Post on 06-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    1/71

    Session 3

    Module 5: COM and Interoperability

    Module 4: Multithreading in C#

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    2/71

    COM and Multithreading / Session3 / 2 of 71

    Module 3 - Review (1)

    Data is stored permanently in the form offiles.

    A stream is a flow of data.

    A stream helps data traveling from onesource point to another. These sourcepoints are file, memory, network, web,string and so on.

    C# provides the System.IOnamespacefor file and stream handling.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    3/71

    COM and Multithreading / Session3 / 3 of 71

    Module 3 - Review (2)

    The BinaryReaderclass is used to read binarydata from a stream.

    The BinaryWriterclass is used for writing binarydata from a C# variable to a specified stream.

    The Stream class is an abstract class from which

    different stream processing classes are being

    derived.

    TheTextReader is the abstract base class forthe StreamReaderand StringReaderclasses.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    4/71

    COM and Multithreading / Session3 / 4 of 71

    Module 3 - Review (3)

    The TextWriter is an abstract base class forclasses that can be used for writing sequentialcharacters.

    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 toperform identical operations as the File class.

    The Directoryclass contains static methods forcreating, moving and enumerating through thedirectories and subdirectories.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    5/71

    COM and Multithreading / Session3 / 5 of 71

    Module 4 - Objectives

    Introduces concept of threads

    Explains how to create threads using C#

    Discusses the Threadclass, its properties,methods and lifecycle.

    Explains about thread priorities and thread

    safety.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    6/71

    COM and Multithreading / Session3 / 6 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 aset 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    7/71

    COM and Multithreading / Session3 / 7 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 ThreadStartdelegates.

    Using the ThreadPoolclass either directly or

    indirectly using asynchronous methods.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    8/71

    COM and Multithreading / Session3 / 8 of 71

    Situations where Threads can be used

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    9/71

    COM and Multithreading / Session3 / 9 of 71

    Situations where not to use Threads

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    10/71

    COM and Multithreading / Session3 / 10 of 71

    Features

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    11/71

    COM and Multithreading / Session3 / 11 of 71

    Benefit

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    12/71

    COM and Multithreading / Session3 / 12 of 71

    Threading namespace

    Multithread programming is possible in .NET

    with the classes and interfaces provided in

    the System.Threadingnamespace.

    The classes and interfaces in the namespace

    help in:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    13/71

    COM and Multithreading / Session3 / 13 of 71

    Classes in System.Threading

    namespace

    The commonly used classes of this

    namespace are list in the table

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    14/71

    COM and Multithreading / Session3 / 14 of 71

    Steps to create threads (1)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    15/71

    COM and Multithreading / Session3 / 15 of 71

    Steps to create threads (2)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    16/71

    COM and Multithreading / Session3 / 16 of 71

    Steps to create threads (3)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    17/71

    COM and Multithreading / Session3 / 17 of 71

    Steps to create threads (4)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    18/71

    COM and Multithreading / Session3 / 18 of 71

    Steps to create threads (5)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    19/71

    COM and Multithreading / Session3 / 19 of 71

    Steps to create threads (6)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    20/71

    COM and Multithreading / Session3 / 20 of 71

    Thread class

    The Threadclass belongs to the

    System.Threadingnamespace.

    TheTh

    readclass can be used to create newthreads, control and manipulate them.

    The Start() method in the Threadclass starts

    the execution of the thread.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    21/71

    COM and Multithreading / Session3 / 21 of 71

    ThreadStart delegate (1)

    .NET Framework carries out multithreadingoperations through the use of delegates.

    The ThreadStartdelegate represents the

    method that will be used to execute onthreads.

    When a thread is being created, an instanceofThreadStart is passed as an argument to

    the constructor of the Threadclass.When the start() method is called on the

    thread object, the object will invoke thedelegate in its own thread of execution.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    22/71

    COM and Multithreading / Session3 / 22 of 71

    ThreadStart delegate (2)

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    23/71

    COM and Multithreading / Session3 / 23 of 71

    Example

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    24/71

    COM and Multithreading / Session3 / 24 of 71

    Properties of Thread class

    The properties ofThreadclass control and

    manage the basic functionality of a thread

    class.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    25/71

    COM and Multithreading / Session3 / 25 of 71

    Example

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    26/71

    COM and Multithreading / Session3 / 26 of 71

    Methods of Thread class

    Some of the common methods are listed in

    the table:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    27/71

    COM and Multithreading / Session3 / 27 of 71

    Example

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    28/71

    COM and Multithreading / Session3 / 28 of 71

    Thread states

    A thread is always in some state or the other right from the time ofits creation until its termination.

    The ThreadState property ofThreadclass defines the state of a

    thread during its execution and provides information about the

    thread status.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    29/71

    COM and Multithreading / Session3 / 29 of 71

    Example

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    30/71

    COM and Multithreading / Session3 / 30 of 71

    Lifecycle of a Thread

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    31/71

    COM and Multithreading / Session3 / 31 of 71

    Thread priorities

    The Priorityproperty ofThreadclassdetermines 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.LowesttoThreadPriority.Highest

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    32/71

    COM and Multithreading / Session3 / 32 of 71

    Example

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    33/71

    COM and Multithreading / Session3 / 33 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    34/71

    COM and Multithreading / Session3 / 34 of 71

    Implementing Thread Safety

    An object can be made thread-safe by

    implementing a minimum of three concepts:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    35/71

    COM and Multithreading / Session3 / 35 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    36/71

    COM and Multithreading / Session3 / 36 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    37/71

    COM and Multithreading / Session3 / 37 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    38/71

    COM and Multithreading / Session3 / 38 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    39/71

    COM and Multithreading / Session3 / 39 of 71

    Thread synchronization

    An object needs to be synchronized when multiple

    threads make calls to its properties and methods.

    Using the Monitorclass of C# is a primary

    mechanism to implement this.

    The commonly used methods of this class as listed

    below:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    40/71

    COM and Multithreading / Session3 / 40 of 71

    Thread pooling with ThreadPool class

    It's cumbersome as well as unsafe for a developer todo all the managing and maintaining of threads.

    C# provides the ThreadPoolclass which gives an

    application a ready set of worker threads in a pool,from which one or more can be chosen for any shorttasks.

    Using the ThreadPoolclass, thread management isdone by the system, the developer can focus onactual application development instead of spendingall the time on managing threads.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    41/71

    COM and Multithreading / Session3 / 41 of 71

    Module 4 - Summary (1)

    A thread is the smallest unit of execution andhas a set of executable instructions.

    .NET framework supports by

    Starting the threads with ThreadStartdelegates. Using the ThreadPoolclass either directly or

    indirectly using asynchronous methods.

    Classes and interfaces for multithreading

    provided in the System.Threadingnamespace.

    .NET Framework carries out multithreadingoperations through the use of delegates.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    42/71

    COM and Multithreading / Session3 / 42 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 ofThreadclassdefines the state of a thread during its

    execution.

    The Priorityproperty ofThreadclass

    determines priority of the thread.

    Initially, the system handles all the threads

    with the same priority.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    43/71

    COM and Multithreading / Session3 / 43 of 71

    Module 4 - Summary (3)

    Thread-safety is attained by using the concept oflocking, and by reducing the possibilities ofinteraction 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 toexclusively access particular sections of code at atime.

    Using the ThreadPoolclass, thread management isdone by the system.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    44/71

    COM and Multithreading / Session3 / 44 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

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    45/71

    COM and Multithreading / Session3 / 45 of 71

    History ofComponent Object Model

    COMis a software technology developed byMicrosoft in 1993 and implemented after

    1997.

    COM comprises of technologies like OLE,OLE Automation,ActiveX, COM+, and

    DCOM.

    The importance ofCOMis to use objects

    across different environments and machines.

    COMpermits reuse of objects without having

    any knowledge of their interior execution.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    46/71

    COM and Multithreading / Session3 / 46 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 orDCOM object is registered within the

    window registry. Language independence is another

    characteristic of COM.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    47/71

    COM and Multithreading / Session3 / 47 of 71

    COM (2)

    During runtime, COM clients and COM objects havethe 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 variouslanguages.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    48/71

    COM and Multithreading / Session3 / 48 of 71

    Characteristics ofCOM

    It separates large systems into subsystems in

    the design phase.

    It helps in implementing each subsystem with

    a component that is created using COM-

    capable tool.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    49/71

    COM and Multithreading / Session3 / 49 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 re-

    use 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    50/71

    COM and Multithreading / Session3 / 50 of 71

    Need forInteroperability

    The internal structure of COM components is

    different from .NET components -> they're not

    compatible.

    The legacy components are exploited byusing the feature of interoperability.

    Interoperability can be done by two ways:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    51/71

    COM and Multithreading / Session3 / 51 of 71

    Situations forInteroperability

    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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    52/71

    COM and Multithreading / Session3 / 52 of 71

    Implementation ofCOMInteroperability

    COM interoperability can be implemented by

    two ways

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    53/71

    COM and Multithreading / Session3 / 53 of 71

    Using COM components from .NET

    The steps to use the COM components

    directly from .NET are:1. Add a reference to the project using Project -> Add

    Reference

    2. Click the COM tab in the Add Reference dialog box

    3. Select the component from the list

    4. 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    54/71

    COM and Multithreading / Session3 / 54 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 oftlbimp.exe is a binary file that isan assembly containing metadata for all the

    types in the original type library.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    55/71

    COM and Multithreading / Session3 / 55 of 71

    Runtime Callable Wrapper (RCW)

    The managed code communicates with COM

    objects through a proxy RCW.

    The primary function of RCW is to connectthe calls between a .NET client and a COM

    object.

    RCW marshals the calls between managed

    and unmanaged codes by hiding the

    differences between them.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    56/71

    COM and Multithreading / Session3 / 56 of 71

    Runtime Callable Wrapper (RCW)

    The RCW can be created by using the utility tlbimpor 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 theRCW object.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    57/71

    COM and Multithreading / Session3 / 57 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 thecalled 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    58/71

    COM and Multithreading / Session3 / 58 of 71

    Interaction between COM component

    and .NET clients using RCW

    Using RCW, the .NET client considers a COM objectas a .NET object.

    .NET client calls to functions are moved to RCW.

    RCW internally calls the native COM functionCoCreateInstance to create a wrapped COM objectand .NET client can accesses that object like nativeobject calls.

    The RCW converts .NET data type into the COM

    compatible data type. Ex: .NET data type, string intoCOM compatible data type BSTR.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    59/71

    COM and Multithreading / Session3 / 59 of 71

    COMCallable 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 referenceddirectly. Therefore, COM clients marshal with

    the managed objects by using CCW as a

    proxy.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    60/71

    COM and Multithreading / Session3 / 60 of 71

    Working ofCCW

    The runtime framework creates one CCW for every

    managed object.

    COM client hold a reference to the CCW by exposing

    the INew interface.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    61/71

    COM and Multithreading / Session3 / 61 of 71

    Interfaces ofCOM callable wrapper

    CCW also provides some interfaces for

    explicit implementation by classes and events

    in the managed environment:

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    62/71

    COM and Multithreading / Session3 / 62 of 71

    Assembly Registration Tool

    (regasm.exe)

    Regasm.exe is a tool that develops a type libraryfrom a .NET assembly.

    Regasm registers type library and its COM classes in

    Windows registry. The main function ofRegasm is to go through the

    metadata in the assembly and add the necessaryentries to the registry.

    After the class is registered, the COM client can use

    it like any other COM class. regasm [options]

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    63/71

    COM and Multithreading / Session3 / 63 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

    2. Use the regasm to register

    regasm /tlb:DotnetComponent.tlb

    DotnetComponent.dll 3. Install the .NET assembly

    gacutil /i DotnetComponent.dll

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    64/71

    COM and Multithreading / Session3 / 64 of 71

    Type Library Exporter (tlbexp tool)

    The two tools that are necessary for COM

    interoperability are Type library exporter and

    importer.

    The main function oftlbexp utility is togenerate a type library for a .NET framework

    assembly that is used by the COM

    components to interop with .NET

    components.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    65/71

    COM and Multithreading / Session3 / 65 of 71

    Need of legacy code and

    unmanaged APIs

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    66/71

    COM and Multithreading / Session3 / 66 of 71

    PlatformInvacation

    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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    67/71

    COM and Multithreading / Session3 / 67 of 71

    Calling unmanaged code

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    68/71

    COM and Multithreading / Session3 / 68 of 71

    Working ofPlatformInvocation

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    69/71

    COM and Multithreading / Session3 / 69 of 71

    Module 5 - Summary (1)

    COMcomprises of technologies like OLE,

    OLE Automation, ActiveX, COM+, and

    DCOM.

    The importance of COM is to use objectsacross 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.

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    70/71

    COM and Multithreading / Session3 / 70 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 typedefinitions present in a COM type library into

    a common language runtime assembly.

    The managed code communicates with COMobjects through a proxy known as Runtime

    Callable Wrapper (RCW).

  • 8/3/2019 Session3 Module4-5 COM Multi Threading

    71/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 itsCOM classes in Windows registry.

    Platform Invocation Services (PInvoke) is a

    technology that allows managed code to call

    unmanaged functions implemented in a DLL.