我有一个非常简单的代码用于测试代理服务器的响应,我希望能够启动一个计时器并停止它并获得这些发生所花费的时间。但是,我不相信我下面的代码是我正在寻找的。 p>
Timer 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();
答案 0 :(得分:45)
要获得经过的时间,StopWatch类可能更容易使用。
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// do stuff
stopWatch.Stop();
long duration = stopWatch.ElapsedMilliseconds;
答案 1 :(得分:11)
以下是使用System.Diagnostics命名空间中的秒表的示例:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
答案 2 :(得分:1)
你是对的。我不认为这就是你要找的东西。你可以这样做:
DateTime 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);