Reputation: 11319
Like a digital clock with hours minutes seconds and milliseconds 00:00:00:00 This is a class i have now:
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;
}
}
}
And i use it like this in another class:
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);
}
What i want is that the second line:
font.DrawText(null, String.Format("{0:N0} fps", this.FPS.GetFPS()), 5, 5, SharpDX.Color.Red);
To be display under the first line and will show the time clock. Maybe i can add the method that show the time in this class and not in the FramesPerSecond class.
How can i make the method and how to display it using the font.DrawText ?
What i tried so far in the FramesPerSecond class i added:
static DateTime _startTime = DateTime.Now;
Then added:
public static TimeSpan RunTime
{
get
{
return DateTime.Now - _startTime;
}
}
Then in the other class to use it i added:
string timeRunning = FramesPerSecond.RunTime.ToString("hh:mm:ss:ff");
And i'm getting exception on this line: 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)
Upvotes: 0
Views: 252
Reputation: 11273
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;
}
}
Just add a DateTime and track between calls, to use it, you just need to use something like this:
string timeRunning = FPS.RunTime.ToString(@"hh\:mm\:ss\:ff");
And then draw that out wherever you want.
Upvotes: 1
Reputation: 125
If you're looking to display a continually updating text with an incremental timer noting time elapsed, the way I've found that works best for me is to add a Timer
Windows Form class to your form, start a Stopwatch
(using System.Diagnostics
), and attach a method to the timer Tick
event to update the label or whatever the text for the clock is held in.
// 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();
}
By default, I believe the Timer has an interval of 100. If you want it to update more more frequently than that I'd recommend 10.
timer1.Interval = 10;
Upvotes: 2