0

Even there are lots of questions and answers about Date Time I couldn't find what I am looking for. Let me explain my problem I created a choronometer with timer(interval 10ms) as "zaman" but my progroam doesn't compiling it as fast as real time chronometer.It doens't give a relaible result(this is also a problem). Therfore I want to use Date Time function to use as a chronometer but there is a problem: I couldn't make them starts from 00:00:00.00(hh:mm:ss.ff). I wanted to do it, because by using time data Program drawing a real time data and for every measurement it must be start from zero until measurement end(elapsed time).Here is my code about by using timer(not relaible one).Can you help how can make it more accurate or how to make date.time.now starts from 00:00:00.00?
salise=miliseconds,saniye=seconds,dakika=minutes

private void zaman_Tick(object sender, EventArgs e)
    {   
            salise = Convert.ToInt32(lblsalise.Text);
            if (salise < 99)
            {
                lblsalise.Text = Convert.ToString(salise + 1);
            }
            else
            {
                lblsalise.Text = "00";
                saniye = Convert.ToInt32(lblsaniye.Text);
                if (saniye < 59)
                {
                    lblsaniye.Text = Convert.ToString(saniye + 1);
                }
                else
                {
                    lblsaniye.Text = "00";
                    dakika = Convert.ToInt32(lbldakika.Text);
                    if (dakika < 59)
                    {
                        lbldakika.Text = Convert.ToString(dakika + 1);
                    }
                    else
                    {
                        lbldakika.Text = "00";
                    }
                }
            }
            lblzaman.Text = lbldakika.Text + ":" + lblsaniye.Text + ":" + lblsalise.Text;



        }
Quanthema
  • 49
  • 2
  • 10
  • 6
    Use `Stopwatch` instead. That's specifically designed for measuring an elapsed time. Beyond that advice, I'm afraid I didn't understand the rest of your question... – Jon Skeet Mar 27 '17 at 10:55
  • What is this code supposed to do? It only shows string manipulations. Where is the current time? Are you trying to use the *label text* to calculate and store hours and minutes instead of using `DateTime.Now` ? – Panagiotis Kanavos Mar 27 '17 at 10:59
  • @Jodrell this won't help at all in this case. The code doesn't try to get the current time at all. It tries to parse and increment labels. – Panagiotis Kanavos Mar 27 '17 at 11:01
  • read this http://stackoverflow.com/questions/4532850/windows-forms-timer-or-system-threading-timer. @PanagiotisKanavos, I believe the code the OP posted is the event handler for a `Windows.Forms` timer. – Jodrell Mar 27 '17 at 11:02
  • @Jodrell that's obvious, but not relevant. The OP is trying to *calculate* the time by incrementing text values. Such code will *always* produce wrong results – Panagiotis Kanavos Mar 27 '17 at 11:05

2 Answers2

2

All timers are skewed which means that trying to calculate elapsed time by incrementing seconds and minutes will result in an invalid value at some point.

Applications use the Stopwatch class to measure elapsed time. This class doesn't count anything. When it starts, it stores the value of a high resolution performance counter in ticks. When you request the Elapsed time, it retrieves the current uptime and calculates the difference.

After that, setting your labels is just a matter of formatting the TimeSpan value returned by the Elapsed property, eg:

Stopwatch _watch=new Stopwatch();

public void Start()
{
    _watch.Start();
}

private void zaman_Tick(object sender, EventArgs e)
{ 
  var time=_watch.Elapsed;
  lblFull.Text =  time.ToString(@"hh\:mm\:ss\.ff");
  lblHours.Text=  time.ToString(@"hh");
  lblMinutes.Text=  time.ToString(@"mm");
  lblSecs.Text=  time.ToString(@"ss");
  lblMs.Text=  time.ToString(@"ff");
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1

I think you be better off with something like this, using a Stopwatch,

var stopwatch = System.Diagnotics.Stopwatch.StartNew();

private void zaman_Tick(object sender, EventArgs e)
{   
    lblzaman.Text = stopwatch.Elapsed.ToString();
}

The value of Stopwatch.Elapsed is a TimeSpan that can be formatted in many different ways, see @Panagiotis's answer for some examples.


If you want lblzaman updated at precise intervals, then you should investigate using System.Timers.Timeror maybe, System.Threading.Timer as your event source, on a background thread but remember, you'll need to handle cross-thread calls on the UI update.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124