计时器上的 C# 已用时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9706803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# elapsed time on a timer?
提问by
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.
我有一段非常简单的代码用于测试来自代理服务器的响应,我希望能够启动一个计时器并停止它并获得这些发生所花费的时间。但是,我不相信我下面的代码是我正在寻找的。
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();
采纳答案by Jason
回答by Shiraz Bhaiji
Here is an example that uses the stopwatch from System.Diagnostics namespace:
这是一个使用 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);
回答by Diego
You are right. I don't think that's what you are looking for. You can simply do:
你是对的。我不认为这就是你要找的。你可以简单地做:
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);

