Search code examples
c#timerstopwatch

Elapsed time on a timer?


I have a really simple bit of code I use to test the response from proxy servers, I want to be able to start a timer and stop it and get the elapsed time it took for those to happen. However, I don't believe the code I have below is what I'm looking for.

var proxyStopWatch = new Timer();
proxyStopWatch.Start();

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());
proxyStopWatch.Stop();

Solution

  • To just get elapsed time, the Stopwatch class will probably be easier to use.

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    
    // do stuff
    
    stopWatch.Stop();
    long duration = stopWatch.ElapsedMilliseconds;