在 WPF 中创建秒表 - 时间精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43044880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:03:40  来源:igfitidea点击:

Creating stopwatch in WPF - time precison

c#wpfmultithreadingtimingdispatchertimer

提问by Krepsy 3

I need to set up a stopwatch in my app. I tried to do it using dispatcher timer, with one millisecond interval. But, the clock runs at about the speed that it takes about ten seconds to count up to a second.

我需要在我的应用程序中设置一个秒表。我尝试使用调度程序计时器来做到这一点,间隔为一毫秒。但是,时钟的运行速度大约需要 10 秒才能计数到一秒。

I assume that it is the threading problem, but how to overcome it?

我认为这是线程问题,但如何克服它?

回答by Erdem K??k

This example can be quite useful for you.

这个例子对你很有用。

public partial class MainWindow: Window   
        {  
            DispatcherTimer dispatcherTimer = new DispatcherTimer();  
            Stopwatch stopWatch= new Stopwatch();  
            string currentTime = string.Empty;  
            public MainWindow()   
            {  
                InitializeComponent();  
                dispatcherTimer .Tick += new EventHandler(dt_Tick);  
                dispatcherTimer .Interval = new TimeSpan(0, 0, 0, 0, 1);  
            }  

            void dt_Tick(object sender, EventArgs e)   
            {  
                if (stopWatch.IsRunning)   
                {  
                    TimeSpan ts = sw.Elapsed;  
                    currentTime = String.Format("{0:00}:{1:00}:{2:00}",  
                    ts.Minutes, ts.Seconds, ts.Milliseconds / 10);  
                    clocktxt.Text = currentTime;  
                }  
            }  

            private void startbtn_Click(object sender, RoutedEventArgs e)  
            {  
                stopWatch.Start();  
                dispatcherTimer .Start();  
            }  

            private void stopbtn_Click(object sender, RoutedEventArgs e)   
            {  
                if (stopWatch.IsRunning)  
                {  
                    stopWatch.Stop();  
                }  
                elapsedtimeitem.Items.Add(currentTime);  
            }  

            private void resetbtn_Click(object sender, RoutedEventArgs e)   
            {  
                stopWatch.Reset();  
                clocktxt.Text = "00:00:00";  
            }  
        }  

回答by Bao Nguyen

Full code.

完整代码。

The Frontend XAML is as follows:

前端 XAML 如下:

<Window x:Class="StopWatch.MainWindow"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  

Title="Simple Stop Watch" Height="350" Width="525">  
<Grid Background="BlanchedAlmond">  
    <TextBlock FontSize="50" Margin="200,-12,175,258" RenderTransformOrigin="1.443,0.195">Timer</TextBlock>  
    <TextBlock x:Name="clocktxtblock" FontSize="70" Margin="118,38,37,183"></TextBlock>  
    <Button x:Name="startbtn" Margin="38,137,350,126" Background="SkyBlue" Content="Start" FontSize="30" Click="startbtn_Click" ></Button>  
    <Button x:Name="stopbtn" Margin="200,137,190,126" Background="SkyBlue" Content="Stop" FontSize="30" Click="stopbtn_Click" ></Button>  
    <Button x:Name="resetbtn" Margin="360,137,28,126" Background="SkyBlue" Content="Reset" FontSize="30" Click="resetbtn_Click" ></Button>  
    <ListBox x:Name="elapsedtimeitem" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="433" Margin="56,199,0,0"/>  
</Grid>  

Coding The CodeBehind File( MainWindows.xaml.cs):

编码 CodeBehind 文件 (MainWindows.xaml.cs):

 public partial class MainWindow: Window   
 {
    DispatcherTimer dt = new DispatcherTimer();  
    Stopwatch sw = new Stopwatch();  
    string currentTime = string.Empty;  
    public MainWindow()   
    {  
        InitializeComponent(); 
        dt.Tick += new EventHandler(dt_Tick);  
        dt.Interval = new TimeSpan(0, 0, 0, 0, 1);  
    }  

    void dt_Tick(object sender, EventArgs e)   
    {  
        if (sw.IsRunning)   
        {  
            TimeSpan ts = sw.Elapsed;  
            currentTime = String.Format("{0:00}:{1:00}:{2:00}",  
            ts.Minutes, ts.Seconds, ts.Milliseconds / 10);  
            clocktxtblock.Text = currentTime;  
        }  
    }  

    private void startbtn_Click(object sender, RoutedEventArgs e)  
    {  
        sw.Start();  
        dt.Start();  
    }  

    private void stopbtn_Click(object sender, RoutedEventArgs e)   
    {  
        if (sw.IsRunning)  
        {  
            sw.Stop();  
        }  
        elapsedtimeitem.Items.Add(currentTime);  
    }  

    private void resetbtn_Click(object sender, RoutedEventArgs e)   
    {  
        sw.Reset();  
        clocktxtblock.Text = "00:00:00";  
    }
}