如何使时间流逝的方法?
我需要一种方法来显示我的过程所花费的时间。我在启动流程时调用它,并在完成流程时再次调用它,该方法打印已用的总时间。
这是我的方法,但始终以 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);
}
}
I need a method that give me the time elapsed awhile my process. I call it at start the process and call it again at finish the process, and the method print the total time elapsed.
This is my method, but always print the time in 00:00. Why is happening this??
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看你在哪里声明
stopwatch
;它是一个局部变量。这意味着您正在创建和使用两个不同的秒表;第一个是在您使用“true”参数调用方法时启动的,然后在方法结束且变量超出范围时释放。第二个被声明,从未启动,然后检查并记录其时间。要解决该问题,请为秒表声明一个实例变量(“字段”)。只要对象存在,它就会保持在作用域内,这意味着它会在方法结束后继续运行,并且当您返回它来停止并检查它时,它仍然是同一个实例。
Look where you're declaring
stopwatch
; it's a local variable. That means you're creating and using two different stopwatches; the first one is started when you call the method with a "true" parameter, then disposed of when the method ends and the variable goes out of scope. The second is declared, never started, then its time examined and logged.To solve the problem, declare an instance variable ("field") for the Stopwatch. That will keep it in scope as long as the object is around, meaning it will keep running after the method ends, and will still be the same instance when you come back to it to stop and examine it.
您的
stopwatch
变量是本地的。当您第二次调用该函数时,它会再次初始化。您需要将声明移至类级别。
Your
stopwatch
variable is local. When you call the function a second time, it's initialized again.You need to move the declaration up to class level.
每次调用此方法时,您都会创建一个新的秒表,但看起来它应该在方法调用之间保持不变。
You're creating a new stopwatch each time this method is called, but it looks like that should be persistent between method calls.
将秒表变量声明放在方法之外。
Take the stopwatch variable declaration outside of the method.
使用怎么样:
What about using:
如果
time
为 true,则您只需启动秒表。如果它是假的,你就永远不会启动它。
更好的结构如下:
注意:
如果您有布尔类型,则不要将其与
true
或false
进行比较。只需直接使用它,如果你想反转它只需使用!
。If
time
is true, you only ever start the stopwatch.If it is false, you never start it.
A better structure would be:
Note:
If you have a boolean type, you don't compare it to
true
orfalse
. Just use it directly and if you want to invert it just use!
.您需要使用 timeSpan.TotalMinutes 而不是 timestamp.Minutes。请参阅 timespan 文档
You need to use timeSpan.TotalMinutes instead timestamp.Minutes. Refer timespan documentation