Krepsy 3
Krepsy 3

Reputation: 73

Creating stopwatch in WPF - time precison

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.

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

Upvotes: 0

Views: 13393

Answers (2)

Erdem Köşk
Erdem Köşk

Reputation: 190

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 = stopWatch.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";  
            }  
        }  

Upvotes: 5

Bao Nguyen
Bao Nguyen

Reputation: 129

Full code.

The Frontend XAML is as follows:

<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):

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

Upvotes: 1

Related Questions