2017-03-27 3 views
0

Je dois configurer un chronomètre dans mon application. J'ai essayé de le faire en utilisant minuterie répartiteur, avec un intervalle d'un milliseconde. Mais, l'horloge tourne à peu près à la vitesse qu'il faut environ dix secondes pour compter jusqu'à une seconde. Je suppose que c'est le problème du filetage, mais comment le surmonter?Création d'un chronomètre dans WPF - Précison de l'heure

+0

Un code serait utile pour trouver votre problème. –

+0

Qu'en est-il de [Classe StopWatch] (https://msdn.microsoft.com/fr-fr/library/system.diagnostics.stopwatch (v = vs.110) .aspx)? – 3615

+0

Dans quel but voulez-vous utiliser 'Stopwatch' (mesurer quelque chose? Application de l'horloge?)? Pouvez-vous montrer un code problématique (* "il faut environ dix secondes pour compter jusqu'à une seconde" *)? Pour mieux comprendre l'imprécision des minuteurs, lisez [this] (http://stackoverflow.com/q/1512294/1997232). – Sinatr

Répondre

0

Cet exemple peut vous être très utile.

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