如何添加一个计算程序运行时间的方法?

时间:2015-04-28 20:20:29

标签: c# .net winforms

类似于小时分秒和毫秒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");

我在这一行得到例外:string 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)

2 个答案:

答案 0 :(得分:2)

如果您希望使用增量计时器显示持续更新的文本并注意时间过去,我发现最适合我的方法是添加Timer Windows窗体类到在您的表单中,启动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;

答案 1 :(得分:1)

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");

然后在任何你想要的地方画出来。