Switch to full style
Codes, tips and tricks,discussions and solutions related to C#
Post a reply

stop watch

Sun May 13, 2007 11:05 pm

Stop watch and timer and alarm, using the Stopwatch class in C#, useful in cases to measure how long your program takes to run:
csharp code
using System;
using System.Diagnostics;
using System.Threading;

class Program
{
static void Main()
{
// Creating an instance of Stopwatch
Stopwatch myWatch = new Stopwatch();

// Start counting
myWatch.Start();

// Any thing that take time
for (int i = 0; i < 200; i++)
{
// Sleep
Thread.Sleep(1);
}

// Stop the watch
myWatch.Stop();

// Write result
Console.WriteLine("Your program took :time elapsed: {0}",
myWatch.Elapsed);
}
}


You can also restart the stop watch using "restart function" :
csharp code
using System;
using System.Diagnostics;
using System.Threading;

class Program
{
static void Main()
{
Stopwatch myWatch = Stopwatch.StartNew();
// Sleep for five seconds
Thread.Sleep(5000);
// Restart the time counter
stop.Restart();
// Sleep again
Thread.Sleep(2000);
// The time difference should be 2 seconds only.
Console.WriteLine(stop.ElapsedMilliseconds);
}
}




Re: stop watch

Fri Mar 27, 2009 12:30 am

can you reupload plzzzzz

Post a reply
  Related Posts  to : stop watch
 Stop animation in JQuery     -  
 Poseidon Infosoft: One Stop Solution for all your IT needs.     -  
 Poseidon Infosoft: One Stop Solution for all your IT needs.     -