簡體   English   中英

如何添加一種可以計算程序運行時間的方法?

[英]How can i add a method that will count the time the program is running?

就像帶有小時,分鍾,秒和毫秒的數字鍾00:00:00:00這是我現在擁有的課程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Capture.Hook
{
    public class FramesPerSecond
    {
        int _frames = 0;
        int _lastTickCount = 0;
        float _lastFrameRate = 0;

        public void Frame()
        {
            _frames++;
            if (Math.Abs(Environment.TickCount - _lastTickCount) > 1000)
            {
                _lastFrameRate = (float)_frames * 1000 / Math.Abs(Environment.TickCount - _lastTickCount);
                _lastTickCount = Environment.TickCount;
                _frames = 0;
            }
        }

        public float GetFPS()
        {
            return _lastFrameRate;
        }
    }
}

我在另一堂課中這樣使用它:

if (this.FPS.GetFPS() >= 1)
                        {
                            font.DrawText(null, String.Format("{0:N0} fps", this.FPS.GetFPS()), 5, 5, SharpDX.Color.Red);
                            font.DrawText(null, String.Format("{0:N0} fps", this.FPS.GetFPS()), 5, 5, SharpDX.Color.Red);
                        }

我想要的是第二行:

font.DrawText(null, String.Format("{0:N0} fps", this.FPS.GetFPS()), 5, 5, SharpDX.Color.Red);

將顯示在第一行下並將顯示時鍾。 也許我可以在此類而不是FramesPerSecond類中添加顯示時間的方法。

我如何制作該方法以及如何使用font.DrawText顯示它?

到目前為止,我在FramesPerSecond類中嘗試的操作是:

static DateTime _startTime = DateTime.Now;

然后添加:

public static TimeSpan RunTime
        {
            get
            {
                return DateTime.Now - _startTime;
            }
        }

然后在另一個類中使用它添加:

string timeRunning = FramesPerSecond.RunTime.ToString("hh:mm:ss:ff");

而且我在這條線上遇到異常:字符串timeRunning = FramesPerSecond.RunTime.ToString(“ hh:mm:ss:ff”);

Error: Error in InitialiseHook: System.FormatException: Input string was not in a correct format.
   at System.Globalization.TimeSpanFormat.FormatCustomized(TimeSpan value, String format, DateTimeFormatInfo dtfi)
   at System.Globalization.TimeSpanFormat.Format(TimeSpan value, String format, IFormatProvider formatProvider)
   at System.TimeSpan.ToString(String format)

如果您想顯示帶有持續時間的增量計時器來不斷更新文本,我發現最適合我的方法是向您的表格添加Timer Windows Form類,請啟動Stopwatch (使用System.Diagnostics ),並將方法附加到計時器Tick事件以更新標簽或保存時鍾的任何文本。

// Don't forget the using in your imports and such
using System.Diagnostics;

Stopwatch stopwatch = new Stopwatch();

public void CheckTime()
{
    label1.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss\\:ff");
}

public void timer1_Tick(object sender, EventArgs e)
{
    CheckTime();
}

默認情況下,我相信計時器的時間間隔為100。如果您希望它的更新頻率比我建議的10更頻繁。

timer1.Interval = 10;

public class FramesPerSecond
{
    int _frames = 0;
    int _lastTickCount = 0;
    float _lastFrameRate = 0;
    DateTime _startTime = DateTime.Now;

    public Timespan RunTime
    {
        get
        {
             return DateTime.Now - _startTime;
        }
     }

    public void Frame()
    {
        _frames++;
        if (Math.Abs(Environment.TickCount - _lastTickCount) > 1000)
        {
            _lastFrameRate = (float)_frames * 1000 / Math.Abs(Environment.TickCount - _lastTickCount);
            _lastTickCount = Environment.TickCount;
            _frames = 0;
        }
    }

    public float GetFPS()
    {
        return _lastFrameRate;
    }
}

只需添加一個DateTime並在兩次調用之間跟蹤,要使用它,您只需要使用以下內容:

string timeRunning = FPS.RunTime.ToString(@"hh\:mm\:ss\:ff");

然后將其繪制到任何您想要的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM