【发布时间】:2011-05-05 15:52:36
【问题描述】:
我需要一种方法,让我在我的过程中流逝一段时间。我在启动进程时调用它,在进程结束时再次调用它,该方法会打印经过的总时间。
这是我的方法,但总是以 00:00 打印时间。为什么会这样??
public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions)
{
var stopwatch = new System.Diagnostics.Stopwatch();
LogBinaryWriter BinaryWriter = new LogBinaryWriter();
string timeElapsed = "";
if(time == true)
{
stopwatch.Start();
}
if (time == false)
{
stopwatch.Stop();
TimeSpan timeSpan = stopwatch.Elapsed;
timeElapsed = (string.Format("\nFile Generated: {0}\nTime Elapsed: {1} minute(s) {2} second(s)",
BinaryWriter.CreateLogFileName(filePath, Convert.ToInt32(logSelected)),
timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n"));
userOptions.DisplayUserMessage(timeElapsed);
}
}
【问题讨论】: