0

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?

Krepsy 3
  • 63
  • 1
  • 2
  • 11
  • Some code would be helpfull to find your problem. – Mighty Badaboom Mar 27 '17 at 11:17
  • What about [StopWatch Class](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx)? – 3615 Mar 27 '17 at 11:19
  • For what purpose you want to use `Stopwatch` (measuring something? clock application?) ? Can you show problematic code (*"it takes about ten seconds to count up to a second"*)? To understand timers inaccuracy better read [this](http://stackoverflow.com/q/1512294/1997232). – Sinatr Mar 27 '17 at 11:24

2 Answers2

5

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";  
            }  
        }  
Prabhdeep Singh
  • 176
  • 1
  • 16
Erdem Köşk
  • 190
  • 1
  • 10
0

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";  
    }
}
Bao Nguyen
  • 119
  • 1
  • 3