Thursday, October 23, 2008

Processes, Threads, AppDomains and Object Contexts

When I was new in .NET, one question always confuses me and that question is: What is the difference between Processes, Threads, Application Domains and Object Context and how they work internally?

I read good articles on these topic one or two weeks back. Today I got time to write so let’s begin…



Process:
Process term used to describe the set of resources and the necessary memory allocations used by a running application. In Windows OS, for each *.exe loaded into memory, the OS create a separate and isolated process.

Using this application isolation approach, OS can manage application more robustly. The result is a much more robust and stable environment, given that the failure of one process does not affect the other process. You can see list of running process using Windows Task Manager in Windows OS.

.NET provides number of classes to interact with processes. The System.Diagnostics namespace defines a number of types that allow you to programmatically interact with processes.


Threads:
Thread is a path of execution within a process.

Every Win32 process has exactly one main “thread” that functions as the entry point for the application and this thread known as Primary Thread.

Processes that contain a single primary thread of execution are intrinsically thread safe, given the fact that there is only one thread that can access the data in the application at a given time. However, a single-threaded process will often appear a bit unresponsive to the user if this single thread is performing a complex operation (such as printing out a lengthy text file, performing a mathematically intensive calculation, or attempting to connect to a remote server located thousands of miles away).

.NET provides facility to spawn additional secondary threads also termed Worker Threads. If worker thread is busy with other operation, the main thread is still responsive to user input, which gives the entire process the potential of delivering greater performance. The System.Threading namespace contains various types that allow you to create multithreaded applications.


Application Domains:
The traditional Win32 application directly hosted within a process, while .NET executables are not hosted directly within a process. Rather, a .NET executable is hosted by a logical partition within a process termed as application domain.

This additional subdivision of process offers sever benefits, some of them are:

• AppDomains are far less expensive in terms of processing power and memory than a fullblown process. Thus, the CLR is able to load and unload application domains much quicker than a formal process.
• AppDomains provide a deeper level of isolation for hosting a loaded application. If one
AppDomain within a process fails, the remaining AppDomains remain functional.

As suggested in previous list, a single process can host any number of AppDomains. Given this fact, be very aware that an application running in one AppDomain is unable to obtain data of any kind (global variables or static fields) within another AppDomain unless they make use of a distributed programming protocol (such as Windows Communication Foundation).

Every process contains at least one application domain which termed as default application domain. This specific application domain is automatically created by the CLR at the time the process launches. After this point, the CLR creates additional application domains on an as-needed basis.

We can also create Application Domain programmatically using System.AppDomain class.


Object Context:
Application domain further subdivided into numerous context boundaries. Using context, the CLR is able to ensure that objects that have special runtime requirements are handled in an appropriate and consistent manner by intercepting method invocations into and out of a given context. For example, if you define a C# class type that requires automatic thread safety (using the [Synchronization] attribute), the CLR will create a “synchronized context” during allocation.

Just as a process defines a default AppDomain, every application domain has a default context. This default context (sometimes referred to as context 0) is used to group together .NET objects that have no specific or unique contextual needs. As you may expect, a vast majority of .NET objects are loaded into context 0. If the CLR determines a newly created object has special needs, a new context boundary is created within the hosting application domain.

Context-agile objects: .NET types that do not demand any special context are termed context-agile objects.

Context-bound objects: .NET types that demand special context allocation are termed context-agile objects.

Wednesday, October 8, 2008

New Features of C#.NET 2008 Language (Part 2)

This is a second part of my post "New Features of C#.NET 2008 Language (Part 1)".


(4) Extension Methods:

As we know, once a type is defined and compiled into a .NET assembly, its definition is, more or less, final. The only way to add new members, update members or remove members is to recode and recompile the code base into an updated assembly.

In C# 2008, using extension methods we can add new functionality to existing complied class without directly updating that class.

This technique can be quite helpful when you need to inject new functionality into classes for which you do not have an existing code base. Using extension methods, you can add functionality to precompiled classes while providing the illusion these methods were there all along.

Restrictions:

(a) Extension methods must be defined within a static class and therefore each extension method must also be declared with the static keyword.
(b) All extension methods are marked as such by using the this keyword as a modifier on the first (and only the first) parameter of the method.
(c) Every extension method can be called either from the correct instance in memory
or statically via the defining static class!

Example:

static class MyExtensions
{
// This method allows any object to display the assembly
// it is defined in.
public static void DisplayDefiningAssembly(this object obj)
{
Console.WriteLine("{0} lives here:\n\t->{1}\n", obj.GetType().Name,
Assembly.GetAssembly(obj.GetType()));
}

// This method allows any integer to reverse its digits.
// For example, 56 would return 65.
public static int ReverseDigits(this int i)
{
// Translate int into a string, and then
// get all the characters.
char[] digits = i.ToString().ToCharArray();
// Now reverse items in the array.
Array.Reverse(digits);
// Put back into string.
string newDigits = new string(digits);
// Finally, return the modified string back as an int.
return int.Parse(newDigits);
}
}

Now that we have these extension methods, so all objects have a new method named DisplayDefiningAssembly(), while System.Int32 class have method named ReverseDigits().

// The int has assumed a new identity!
int myInt = 1234;
myInt.ReverseDigits();

// So has the DataSet!
System.Data.DataSet ds = new System.Data.DataSet();
ds.DisplayDefiningAssembly();


(5) Object Initializer Syntax:
Using this technique, it is possible to create a new type variable and assign a slew of properties and/or public fields in a few lines of code. Syntactically, an object initializer consists of a comma-delimited list of specified values, enclosed by the { and } tokens. Each member in the initialization list maps to the name of a public field or public property of the object being initialized.

Example:
Class A
{
private int customerId;
private string customerName;
public CustomerId
{
Get { return customerId; }
Set { customerId = value; }
}
public CustomerName
{
Get { return customerName; }
Set { customerName = value; }
}
}

Now, you can declare object of class A as follow:
A objA = new A{CustomerId = 1, CustomerName = “XYZ};

Collection Initialization:

This syntax makes it possible to populate a container (such as ArrayList or List) with items using a syntax that models that of a simple array. Consider the following examples:


// Init a standard array.
int[] myArrayOfInts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init a generic List<> of ints.
List myGenericList = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init an ArrayList with numerical data.
ArrayList myList = new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };


(6) Anonymous Types:

There are other times in programming when you would like to define a class simply to model a set of encapsulated (and somehow related) data points without any associated methods, events, or other custom functionality. Furthermore, what if this type is only used internally to your current application and it’s not intended to be reused? If you need such a “temporary” type, earlier versions of C# would require you to nevertheless build a new class definition by hand:

internal class MyClass
{
// Define a set of private member variables...
// Make a property for each member variable...
}

As of C# 2008, we are now provided with a massive shortcut for this very situation termed anonymous types, When you define an anonymous type, you do so by making use of the new var keyword in conjunction with the object initialization syntax. E.g.

var myCust = new {customerId = 1, customerName = “XYZ”};

At compile time, the C# compiler will autogenerate a uniquely named class on our behalf. Given the fact that this class name is not visible from C#, the use of implicit typing using the var keyword is mandatory.

Limitations of anonymous type:

• You don’t control the name of the anonymous type.
• Anonymous types always extend System.Object.
• The fields and properties of an anonymous type are always read-only.
• Anonymous types cannot support events, custom methods, custom operators, or custom overrides.
• Anonymous types are always implicitly sealed.
• Anonymous types are always created using the default constructor.

Tuesday, October 7, 2008

New Features of C#.NET 2008 Language (Part 1)

Everbody wondering Microsoft releasing new version of .NET very frequently so it is some what difficult to learn new features. I was thinking to write artical on new features of C#.NET 2008 language. Today I got some time to write so lets starts with summary of new features.

Followings are the new features of C#.NET 2008:



  1. Implicitly Typed Local Variables
  2. Automatic Properties
  3. Partial Methods
  4. Extension Methods
  5. Object Initializer Syntax
  6. Anonymous Types

(1) Implicitly Typed Local Variables:
C# 2008 provides a new keyword var, which can use in place of specifying a formal data type (such as int, string, double). So now you can declare variable as follow:

var myCounter = 15;
var myBool = true;

When you declare variables as above, the compiler will automatically infer the underlying data types based on the initail value used to initialize the variables.
You can use this implicit typing for any type including arrays, generic types and your own custom types.

Limitations of Implicitly Typed Variables:
(1) Implicity typing applies only to local varibles in a method or property scope.
(2)We can not use var keyword to define return values, parameters or field data of a type.

// Error! var cannot be used as field data!
private var myInt = 10;
// Error! var cannot be used as a return value or parameter type
public var MethodName(var a, var b){}

(3)Local variable declared with var keyword must be assigned an initial value at the time of declaration and cannot be assigned the initial value of null.

//Error! Must assign a value
var myVar
// Error! Must assign value at exact time of declaration!
var myVar;
myVar = 0;
// Error! Can't assign null as initial value!
var myObject = null;

(4) It is illegal to define a nullable implicitly typed local variable using the C#? token.

You have seen syntax and limitations of implicitly typed local variables, I am sure you are wondering when to make use of this construct as var keyword is more confusing then int keyword for declaring integer variable. So the real use of implicitly typed local variable is in LINQ.

(2) Automatic Properties:
As a developer, we must know the use of properties. Generally we creates properties to give access of our class or interface to outside world.
C# property syntax is not too problematic but just imagine if you are modeling a class that requires 20 properties then you have to declare 20 private variables (backing fields) for 20 properties which requires lots of typing.
C# 2008 provides solution of this problem by providing feature of Automatic Properties. As the name implies, this feature will offload the work of defining a private backing field and the related C# property member to the compiler using a new bit of syntax.


e.g.
class Customer
{
// Automatic Property Syntax
public string CutomerName {get; set;}
}


When defining automatic properties, you simply specify the access modifier, underlying data type, property name and empty get/set scopes. At compile time, your class will be provided with an autogenerated private backing fields.

One more thing about automatic property, it is not possible to build read-only or write-only automatic properties.


// Read-only property? Error!
public int ReadOnlyProperty { get; }
// Write only property? Error!
public int WriteOnlyProperty { set; }


(3) Partial Methods:
As we know, .NET 2.0 provides facility of partial class which allows us to partition the full implementation of a class across multiple code files.
C# 2008 widens the scope of the partial keyword in that it can now be applied on the method level. This allows you to prototype a method in one file and implementation in another file. However C# partial methods have a number of following restrictions:

• Partial methods must return void.
• Partial methods can not have arguments with the out modifier.
• Partial methods are always implicitly private.
• Partial methods can be static or instance level.
• Partial methods can only be defined within a partial class.

Example:
Partial class A
{
public int sum(int a, int b)
{
varifynumbers(int a, int b);
}
//Partial method
partial void verifynumbers(int a, int b);
}

Implementation of verifynumers method in another file:
partial class A
{
partial void verifynumbers(int a, int b)
{
//some logic here
}
}

Uses of Partial Methods:
From above limitations, it is hard to see many useful applications of this new langauge feature. The truth is partial methods will more likely used feature!!!
Consider verifynumber() method of our example perform some very intensive calculations. By marking this method with the partial modifier, other class builders have the option of providing implementation details if they so choose. In this case, partial methods provide a cleaner solution than using preprocessor directives, supplying “dummy” implementations to virtual methods or throwing NotImplementedException objects.



In some ways, C# partial methods are a strongly typed version of conditional code compilation (via the #if, #elif, #else, and #endif preprocessor directives).

You can find second part of this post at New Features of C#.NET 2008 Language (Part 2).

BOOKS OF VB.NET 2008, C#.NET 2008, SQL SERVER 2008

Interested in 2008 series of .NET and SQL Server??? To download books of VB.NET, C#.NET and SQL Server click below icon:

Monday, October 6, 2008

Get HardDrive serial number in .NET

This is my first post in this blog which is related to getting HardDrive serial number in .NET. Getting serial number of HardDrive in .NET application is very hot topic in developer community. Mostly developer needs HardDrive serial number to avoid piracy of windows based software as I have used this idea 3 yrs. back in my windows application.


In .NET, we can not get hardware information in depth as we were in C/C++. Because of this limitation we need to use C/C++ exe or dll in our .NET application.There are many ways to use non .NET dll in .NET applications. One of famous way is to create COM component in VC++ and use that COM in our .NET application but I have not used this way becuase I don't know how to create COM component in VC++. For sake of simplity of VC++ dll I had created simple API dll in VC++ and make API call through VB.NET application


To call API dll from VB.NET, we need to follow some specific steps. I have written those steps below:

  1. Put API dll in same folder where your application's exe reside.
  2. Add reference of System.Runtime.InteropServices in the class from where you wants call API function.
  3. Add <DllImport("API Dll Name")> attribute before function name which calls the API function. This function must be Shared.
  4. e.g.
  5. <DllImport("API Dll Name")> _
    Public Shared Function getHardDriveComputerID() As String
    End Function
    'Leave function empty - DLLImport attribute forces calls to function of API dll.
    Call getHardDriveComputerID() function from any class of your application as per your requirement.

In our case, I have used API dll named "Win32.dll" (I found this dll from googling) in my VB.NET application. This dll is works for following versions of Windows:

  • Windows 98
  • Windows 2000
  • Windows NT
  • Windows XP

Below compressed file contains sample application.