Quanthema
Quanthema

Reputation: 59

Date Time as Choronometer

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;



        }

Upvotes: 0

Views: 328

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131180

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

Upvotes: 2

Jodrell
Jodrell

Reputation: 35696

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.

Upvotes: 1

Related Questions