如何在C#中正确使用Timespan()构造函数

时间:2013-08-11 14:53:59

标签: c# windows windows-phone-8

我正在为Windows Phone 8开发秒表,但实际上我无法正确获得计时功能。我想在每1000毫秒后更新第二个,但由于我是C#的新手,我无法做到正确。这是我现在的代码。 :/

using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Milli_Stopwatch;
using System.Windows.Threading;


namespace Milli_Stopwatch
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor

        DispatcherTimer timer = new DispatcherTimer();

        int millisec = 00;
        int sec = 00;
        int min = 00;
        int hour = 00;

        Boolean check = false;
        public MainPage()
        {
            InitializeComponent();

            timer.Interval = new TimeSpan(0,0,0,0,1);
            timer.Tick += new EventHandler(DispatcherTimer_tick);

        }

        private void DispatcherTimer_tick(object sender, EventArgs e) 
        {
            if (millisec == 1000) 
                { 
                    millisec = 00; 
                    sec = sec + 1; 
                    Secblock.Text = sec.ToString(); 
            }

            if (sec == 59)
            {
                sec = 00;
                min = min + 1;
                Minblock.Text = min.ToString();
            }

            if (min == 59)
            {
                min = 00;
                hour = hour + 1;
                Hourblock.Text = hour.ToString();
            } 

            millisec = millisec + 1;
            Millisecblock.Text = millisec.ToString();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (check == false)
            {
                timer.Start();
                check = true;
                startbutton.Content = "STOP";
            }
            else {
                timer.Stop();
                check = false;
                startbutton.Content = "START";
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            timer.Stop();
            check = false;
            startbutton.Content = "START";
            millisec = 000;
            sec = 00;
            min = 00;
            hour = 00;

            Millisecblock.Text = "00";
            Secblock.Text = "00";
            Minblock.Text = "00";
            Hourblock.Text = "00";

        }



    }
}

2 个答案:

答案 0 :(得分:1)

当秒到达60而不是59时,时钟进入下一分钟。相同的分钟到小时。你得到它有点毫秒,但之后通过增加它搞砸了它。

无论如何你不应该这样做,计时器不够准确,你总是落后。启动时存储DateTime.UtcNow的值。当计时器滴答时,从DateTime.UtcNow中减去该值。现在您有一个准确的TimeSpan,它始终与经过的挂钟时间相匹配,并且不受定时器精度的影响。

答案 1 :(得分:0)

首先,您要将时间跨度初始化为1毫秒。恕我直言,使用TimeSpan.FromMilliseconds(1);代替构造函数要容易得多。

其次,您不能完全依赖定时器已用事件发生,因此为了使您的秒表正确,您需要使用DateTime.Now - {the DateTime.Now from when you started the timer}之类的东西并解析它

DateTime startedTimeStamp = DateTime.Now;

然后当你处理经过的事件时。

txtTime.Text = (DateTime.Now - startedTimeStamp).ToString("G");

无需处理小时,分钟,秒,毫秒等的每个场景......或其中任何一个。框架将为您处理它。