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.
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.
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.
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
Starting the threads with ThreadStart delegates. Using the ThreadPool class either directly or indirectly using asynchronous methods.
Features
Benefit
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:
The commonly used classes of this namespace are list in the table
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.
Example
The properties of Thread class control and manage the basic functionality of a thread class.
Example
Example
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.
Example
Lifecycle of a Thread
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
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.
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.
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.
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.
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:
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
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
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
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.
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.
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
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
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.
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
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.
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.
CCW also provides some interfaces for explicit implementation by classes and events in the managed environment:
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.
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
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.