Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm counting ticks from a timer which I use to send a double to a custom function which updates a label with text in the format of 00h : 00m : 00s . 000 ms :

C#
private string TimeRetrieval(double milliseconds)
{
    TimeSpan tmspan = TimeSpan.FromMilliseconds(milliseconds);

    string timelapse = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",
    tmspan.Hours,
    tmspan.Minutes,
    tmspan.Seconds,
    tmspan.Milliseconds);


    return timelapse;
}


It works but what I only want to display from hours to tenths of a second, like this: 00:00:00.0 not exact milliseconds. I have Googled and Binged and search and sought for days for what should be a simple answer. Some code accepted in other communities gives errors prior to compilation. I am using an older laptop so I'm running XP SP3 with C# 2008 ee and .NET 4.0. 4.0 supports custom formatting of TimeSpans so I'm a bit confuzzled as to why I'm having issues using code which works for others. I've read up on the .f parameter but it doesn't work for me. Am I missing a reference ? Could someone please explain how to round properly to the nearest tenth of a second ? If it helps to know I am making a custom video player and this will be the playback marker reference. Also what is the proper value for timer.interval so that 1 second is counted at the proper speed ? I thank you for your help.
Posted

1 solution

You mention "ticks," but your input parameter is a double named 'milliseconds. Since there are 10,000 ticks per millisecond; a tenth of a second would be 100 milliescond == 1 millio ticks, I wonder if you are actually passing in the 'ticks value. Are we on the same page here ?

There's "precision," and then there's "accuracy:" in .NET; those terms need to be carefully considered in coding: this article may be useful in your understanding the possible lack of accuracy with using a WinForms Timer Control ... or using DateTime Struct, or using DateTime.Now: [^].

If you really need the best accuracy available, at the millisecond level, I suggest you switch to using the 'StopWatch object which has possible nanosecond precision: [^].

"I'm a bit confuzzled as to why I'm having issues using code which works for others." For this type of question, it is helpful if you shown a concrete example of your code as is, and clearly show the current result, and the expected result.
 
Share this answer
 
Comments
John Hardeman 7-Mar-15 0:12am    
I thank you for the attempt but I believe you misunderstood my question. I require less accuracy not more which is why I ask about rounding. It also allows me to use the simpler timer instead of a stopwatch. I didn't refer to 'tick' in the true accurate sense. I know a cpu 'tick' is == 100ns but a timer tick is == 1 ms. If I were to set my timer interval at 1000 then it would generate a timer tick every 1000 ms or 1 second. I am pulling a value from a timer so a real tick doesn't matter. I should be able to fine tune it with a setting of 100 but it doesn't count right. My focus is how to display the information.

01:33:42.7 not 01:33:42.7890123456789 blah blah blah.
I already have the code working as stated in my post, I just want to display it properly. I've tried Math.Round, string.Format, whichever. If someone can provide me with a better string.Format method then I'll gladly use it.

For thoroughness here is my complete code for the dialog with the timer and label:

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace timer
{
public partial class Form_Main : Form
{
public Form_Main()
{
InitializeComponent();
}

// begin global variables
double countCapture = 0;
// end global variables

private void Form_Main_Load(object sender, EventArgs e)
{
timer_timelapse.Interval = 1; // 100 ms
}

private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
{
timer_timelapse.Stop();
}

private void timer_timelapse_Tick(object sender, EventArgs e)
{
countCapture++; // count our ticks
label_timelapse.Text = TimeRetrieval(countCapture);
}

private void button_Start_Click(object sender, EventArgs e)
{
timer_timelapse.Start();
}

private void button_Stop_Click(object sender, EventArgs e)
{
timer_timelapse.Stop();
}



// begin custom functions
//
// begin TimeRetrieval(int milliseconds)
private string TimeRetrieval(double milliseconds)
{
TimeSpan tmspan = TimeSpan.FromMilliseconds(milliseconds);

string timelapse = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",
tmspan.Hours,
tmspan.Minutes,
tmspan.Seconds,
tmspan.Milliseconds);


return timelapse;
}

private void button1_Click(object sender, EventArgs e)
{
double p = 10.75;
double q = Math.Round(p, 1); // returns 10.8
MessageBox.Show(p.ToString() + Environment.NewLine +
q.ToString() + Environment.NewLine,
@"title");
}
// end TimeRetrieval(int milliseconds)
//
// end custom functions
}
}
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900