如何使时间流逝的方法?

发布于 2024-11-05 07:24:07 字数 955 浏览 1 评论 0原文

我需要一种方法来显示我的过程所花费的时间。我在启动流程时调用它,并在完成流程时再次调用它,该方法打印已用的总时间。

这是我的方法,但始终以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

巾帼英雄 2024-11-12 07:24:07

看看你在哪里声明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.

嗳卜坏 2024-11-12 07:24:07

您的 stopwatch 变量是本地的。当您第二次调用该函数时,它会再次初始化。

您需要将声明移至类级别。

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions)
{
   ... etc

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.

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions)
{
   ... etc
澉约 2024-11-12 07:24:07

每次调用此方法时,您都会创建一个新的秒表,但看起来它应该在方法调用之间保持不变。

You're creating a new stopwatch each time this method is called, but it looks like that should be persistent between method calls.

莫多说 2024-11-12 07:24:07

将秒表变量声明放在方法之外。

Take the stopwatch variable declaration outside of the method.

南薇 2024-11-12 07:24:07

使用怎么样:

var startTime = DateTime.Now;

... your code

var elapsed = DateTime.Now - startTime;

What about using:

var startTime = DateTime.Now;

... your code

var elapsed = DateTime.Now - startTime;
暗藏城府 2024-11-12 07:24:07
    if(time == true)
    {
        stopwatch.Start();
    }
    if (time == false) 
    {
        stopwatch.Stop(); 
        ...
    }

如果 time 为 true,则您只需启动秒表。

如果它是假的,你就永远不会启动它。

更好的结构如下:

if(time)
{
    stopwatch.Start();
}

... //code to measure here

if (time) 
{
    stopwatch.Stop(); 
    // log elapsed time
}

注意:

如果您有布尔类型,则不要将其与truefalse 进行比较。只需直接使用它,如果你想反转它只需使用 !

    if(time == true)
    {
        stopwatch.Start();
    }
    if (time == false) 
    {
        stopwatch.Stop(); 
        ...
    }

If time is true, you only ever start the stopwatch.

If it is false, you never start it.

A better structure would be:

if(time)
{
    stopwatch.Start();
}

... //code to measure here

if (time) 
{
    stopwatch.Stop(); 
    // log elapsed time
}

Note:

If you have a boolean type, you don't compare it to true or false. Just use it directly and if you want to invert it just use !.

各自安好 2024-11-12 07:24:07

您需要使用 timeSpan.TotalMinutes 而不是 timestamp.Minutes。请参阅 timespan 文档

You need to use timeSpan.TotalMinutes instead timestamp.Minutes. Refer timespan documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文