Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# elapsed time on a timer?

Tags:

c#

To just get ellapsed time, the StopWatch class will probably be easier to use.

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

// do stuff

stopWatch.Stop();
long duration = stopWatch.ElapsedMilliseconds;

Here is an example that uses the stopwatch from System.Diagnostics namespace:

var stopWatch = new Stopwatch();
stopWatch.Start();

Thread.Sleep(10000);
stopWatch.Stop();

// Get the elapsed time as a TimeSpan value.
var ts = stopWatch.Elapsed;

// Format and display the TimeSpan value.
string elapsedTime = $"{ts.Hours}:{ts.Minutes}:{ts.seconds}.{ts.Milliseconds / 10}",
    
Console.WriteLine("RunTime " + elapsedTime);

You are right. I don't think that's what you are looking for. You can simply do:

var start = DateTime.Now;
string[] splitProxy = ProxyList[i].Split('|');

string testResults 
     = HTMLProcessor.HTMLProcessing.HTMLResults("http://www.google.com", 
     splitProxy[0], Convert.ToInt32(splitProxy[1]), true, out testResults);
ProxyListResults.Add(ProxyList+"|"+proxyStopWatch.Interval.ToString());

Console.WriteLine("Time elapsed in milliseconds was: " + 
    (DateTime.Now - start).TotalMilliseconds);