Sunday, April 5, 2015

Use of Robocopy in C# application

Robocopy is a robust file copy tool from Microsoft introduced from Windows Server 2003. It is very robust in copy operation having other good features.

Please go through these articles for more details. Article 1 & Article 2

One can integrate this Robocopy tool in c# .net applications to copy files (large). Below code shows usage.

 
This code will copy files inside source folder into target folder.

Friday, March 13, 2015

Call ASP.Net Web Api using WebRequest & WebResponse

I was trying to find a way to call web api from my console application. I came across with .net abstract class WebRequest (link) which can call web api. It has one derived class HttpWebRequest (link) which has more helpful properties to call web api. So, I will use this here. We use WebResponse class (link) to receive response.

Sample Code:























Note: One can use this code to access any URL. I have used web api as example here.

Thursday, January 29, 2015

Difference between Thread.Sleep and Thread.SpinWait in C# .NET

Thread.Sleep:

Thread.Sleep method used to put a thread to sleep for a specified timeout and thread will be put in special queue of threads for waiting. Contextswitching will happen and other thread will get chance to process. After specified time elapsed, slept thread will be again awakened by operationg system.

Example:
Thread.Sleep(10); //Thread will sleep for 10 milliseconds


Thread.SpinWait

It will wait thread by specifying iteration[CPU] count.

Example:
Thread.SpinWait(10000); //Thread will wait for 10000 iterations to complete


As per example, current thred will wait for 10000 iteration to complete. Time required will be depend on CPU processing power.

Thursday, January 22, 2015

C# as Programming Language

Microsoft has introduced C# as coding language in 2000. It is a simple & easy object oriented programming language.

For more details about language please go through below links...

https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx
https://msdn.microsoft.com/en-us/vstudio/hh341490