Tuesday, March 23, 2010

IIS Error: The process cannot access the file because it is being used by another process.

Recently in our one of the web servers all the sites using port 80 were stopped and when we tried to restart website it was giving error like process cannot access the file because it is being used by another process. After analyzing and diagnosis and searching on internet for the same problem i have found that it was a problem due to port 80 was used by another process. To solve the problem i have following command which will list IPs,PID(Process ID) and port that used by the process. The command is as below.

NETSTAT -ano
After running that command in command in command prompt it will give output like following.

IIS Error, IIS 6.0, IIS 7.0

From that output you can fine which port is used by which PID(Process Id). And then you can find the process from task manager. Process Id column in task manager is not enabled by default so you can do by View Menu->Select Column a following dialog box will appear.

ProcessId

Select PID (Process Identifier) and press OK.Now process Id will appear in task manager and in task manger go to processes tab where you can find out process by checking process id column then select that process right click->click End Process that will kill that process. Now again try to restart the IIS Site it will restart. In my case it was java updater who is using same port 80 and after killing my IIS Sites were ok. Hope this will help you..

Technorati Tags: ,

Shout it
kick it on DotNetKicks.com
Share:
Monday, March 22, 2010

C# 4.0-string.IsNullOrWhiteSpace()

We already have string.IsNullOrEmpty() method to check whether the string is null or empty.Now with Microsoft.NET Framework 4.0 there is a new another method that is called string.IsNullOrWhiteSpace() which will also check whether string is having whitespaces or not with null and empty string.This method is help full where we can check string is having whitespace rather then its empty or null. It will return a true if a string is empty,null or its having whitespaces. Let’s create a simple example and will see how its works. Here we are going test it with 4 options Empty string,Normal String,White spaces and Null String. Following is a code for that.

class Program
{
static void Main(string[] args)
{
string TestString = string.Empty;
Console.WriteLine("Test with empty string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = "Normal String";
Console.WriteLine("Test with normal string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = " ";
Console.WriteLine("Test with whitespace:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = null;
Console.WriteLine("Test with null:{0}",
string.IsNullOrWhiteSpace(TestString));
Console.ReadLine();
}
}
Following is a output of the above code here you can see its returning false with Normal string and for all other options its returning true as expected.
image

Shout it
kick it on DotNetKicks.com
Share:
Sunday, March 21, 2010

C#-Constructors,Static Constructors and Destructors Execution in Inheritance

While taking interview for .NET Technologies i often ask about the execution sequence of the constructor and destructor in inheritance But from the my experience i have found that lots of people are still confused with execution sequence of constructor and destructors. Lets create a simple example and learn some basic things that is very important while using inheritance in C#.

  • Constructors will be executed in from parent to child sequence means first parent class constructor will be executed then after that child class constructor will be executed.
  • Destructors execution order is reverse then constructors first it will execute child class destructor and then it will execute the parent class destructor.
  • Static constructors are different then the normal constructors and its executes when first object of class is created it will be executed. Most of people are very confused this kind of scenario in inheritance. Here scenario will be like when the first object of child class created then it will execute the child class static constructor and then after the parent class static constructor is executed. After that it will never got executed.

Lets create a simple class which will illustrate the above worlds. First lets create a class A with constructor,destructor and a static constructor.

public class A
{
static A()
{
System.Console.WriteLine("A Static Constructor");
}
public A()
{
System.Console.WriteLine("A public constructor");
}

~A()
{
System.Console.WriteLine("A Destructor");
}
}
Now we will inherit this class with the another class B which is also having constructor,destructor and a static constructor.
public class B : A
{
static B()
{
System.Console.WriteLine("B Static Constructor");
}
public B()
{
System.Console.WriteLine("B public constructor");
}

~B()
{
System.Console.WriteLine("B Destructor");
}
}
Now lets create two objects of Class B in main function of our console application to see how constructors works and after that we will destroy the object via assigning null values and then forcefully we will do Garbage Collection via GC.Collect() to see how destructors works below is the code for that.
 class Program
{
static void Main(string[] args)
{
B b1 = new B();
B b2 = new B();
//code return to destroy object of a class
b1=b2 = null;
GC.Collect();
Console.ReadLine();

}
}
After running the console application output will be as follows.

Construcotr-Destructor in Ineritance,C#

As you can see static constructors are only executed when the first object of a class is created and Constructors are executed like parent to child way and in reverse destructors are executed from child to parent way. Hope this will help you understand execution sequence of constructor in inheritance.

Shout it
kick it on DotNetKicks.com
Share:

Singleton Class in C#

Singleton pattern is very popular pattern used by programmer. The idea behind the singleton pattern is to have only one instance of a class at any time. This kind of pattern is very useful for for heavy resources like Linq Data Context etc. It is also useful in multithreaded application where different thread of application try to crate instance of one class but it should be thread safe. It can be also useful for global resources and state objects. In singleton class there are two things required. One static variable which hold the instance of class and other is a static method which will return the instance of singleton class. Here is the example of singleton class

 public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
return mySingleTonInstance;
}

}
Now we are done with calling single ton class like following.
 MySingleTon objSingleTon = MySingleTon.GetInstace(); 
The above example is not thread safe so it may be possible that different thread can create different instance of classes. To make above singleton class thread safe we need to change code like following.
   public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
lock (typeof(MySingleTon))
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
return mySingleTonInstance;
}

}
}
Hope this will help you…

Shout it
kick it on DotNetKicks.com
Share:
Saturday, March 20, 2010

Visual Studio 2010-Automatically Adjust Visual Experience.

Visual studio 2010 is great IDE and i am discovering everyday some thing new. Today i have discovered one of fantastic option to increase performance of your visual studio IDE. Visual Studio 2010 has option when we enabled it it will automatically adjust the visual and client experience as per your hardware configuration. It will increase the performance of visual studio and productivity in lower hardware also. You can find that checkbox under Tools->General-> Automatically adjust visual experience based on client performance. There are two checkbox is given one for above and another for Rich User Experience but they may decrease your visual studio performance. Like following.

Visual Studio 2010, User Experience

Shout it
kick it on DotNetKicks.com
Share:

Indexer class in C#

While taking interviews for Microsoft.NET i often ask about indexer classes in C#.NET and i found that most of people are unable to give answer that how we can create a indexer class in C#.NET. We know the array in C#.NET and each element in array can be accessed by index same way for indexer class a object was class can be accessed by index. Its very easy to create a indexer class in C#. First lets looks some points related to indexer class in C#.

  • Indexer class object can be accessed by index only.
  • We can use same mechanism to access object of indexer class as we are doing it for array.
  • You can can specify any valid C# type as return type of indexer classes.
  • You must have to create a property with parameter using this keyword for indexer class.
  • You can also implement multi parameter indexer also.

Let’s create simple indexer class with single parameter and for string type.

 public class MyIndexer
{
private string[] MyIndexerData;
private int SizeOfIndexer;

//declaring cursor to assing size of indexer
public MyIndexer(int sizeOfIndex)
{
this.SizeOfIndexer = sizeOfIndex;
MyIndexerData = new string[SizeOfIndexer];
}

public string this[int Position]
{
get
{
return MyIndexerData[Position];
}
set
{
MyIndexerData[Position] = value;
}
}
}
Now we done with indexer class and lets create object of that indexer class and will see output of our console application also. Following is the code for that.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indexSize=5;
MyIndexer objMyIndexer = new MyIndexer(indexSize);
for (int i = 0; i < indexSize; i++)
{
objMyIndexer[i] = i.ToString();
Console.WriteLine("Indexer Object Value-[{0}]: {1}",
i, objMyIndexer[i]);
}
Console.ReadKey();
}
}
}
and Here is the output for the indexer class console application.

Indexer Class Example in C#,Indexer Class,C#

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Microsoft.NET 4.0/VB.NET 10 Automatic Properties

In C# we are having automatic properties since C# 3.5 framework but now with Microsoft.NET 4.0 Framework VB.NET 10.0 version we are also having automatic properties for VB.NET Also.

Like in C# we can define automatic like following.

Public string TestProperty
{
get;
set;
}
Same way we can define in automatic Property in VB.NET as follows.
Public Property TestProperty As String 
You can use this properties any where in class same as C# automatic property.

Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews