C#計時利器:Stopwatch的使用

      本文演示C#計時器:Stopwatch的使用。

     頁面代碼:

<Window x:Class="TestStopwatch.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="265" Width="380">
    <Grid>
        <Button Content="開始" Height="23" HorizontalAlignment="Left" Margin="22,80,0,0" Name="btn_Start" VerticalAlignment="Top" Width="75" Click="btn_Start_Click" />
        <Label Content="計時時間:" Height="28" HorizontalAlignment="Left" Margin="22,25,0,0" Name="label1" VerticalAlignment="Top" />
        <Button Content="停止" Height="23" HorizontalAlignment="Left" Margin="22,118,0,0" Name="btn_Stop" VerticalAlignment="Top" Width="75" Click="btn_Stop_Click" />
        <Button Content="重新開始" Height="23" HorizontalAlignment="Left" Margin="22,192,0,0" Name="btn_Restart" VerticalAlignment="Top" Width="75" Click="btn_Restart_Click" />
        <TextBlock Height="28" FontSize="20" HorizontalAlignment="Left" Margin="87,25,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="119" />
        <Button Content="重置" Height="23" HorizontalAlignment="Left" Margin="22,155,0,0" Name="btn_Reset" VerticalAlignment="Top" Width="75" Click="btn_Reset_Click" />
        <TextBlock FontSize="14" Height="23" HorizontalAlignment="Left" Margin="103,192,0,0" Name="textBlock2" Text="Restart()=Reset()+Start()" VerticalAlignment="Top" Width="214" />
    </Grid>
</Window>
後臺代碼:

using System;
using System.Windows;
using System.Timers;
using System.Diagnostics;

namespace TestStopwatch
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Timer _timer;
        private Stopwatch _stopWath;
        public MainWindow()
        {
            InitializeComponent();
            _timer = new Timer(500);
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Start();
            _stopWath = new Stopwatch();

        }

        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //查看計時器的時間
            this.Dispatcher.Invoke((Action)delegate
            {
                this.textBlock1.Text = _stopWath.Elapsed.Minutes.ToString() + ":" + _stopWath.Elapsed.Seconds.ToString() + ":" + _stopWath.Elapsed.Milliseconds.ToString();

            });
        }


        private void btn_Start_Click(object sender, RoutedEventArgs e)
        {
            _stopWath.Start();
        }

        private void btn_Stop_Click(object sender, RoutedEventArgs e)
        {
            _stopWath.Stop();
        }

        private void btn_Restart_Click(object sender, RoutedEventArgs e)
        {
            _stopWath.Restart();
        }

        private void btn_Reset_Click(object sender, RoutedEventArgs e)
        {
            _stopWath.Reset();
        }
    }
}

       經過測試發現Stopwatch還是挺好用的,其中的Restart方法等於Reset+Start。


源代碼:http://download.csdn.net/detail/jiangzhanchang/4520665

轉載請隨便。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章