using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { int seconds = 0; int tickCounter=0; public MainWindow() { InitializeComponent(); } void timer1_Tick(object sender, EventArgs e) { tickCounter ++; if (tickCounter == 1000) { tickCounter = 0; seconds++; label1.Content = seconds.ToString(); } } private void button1_Click(object sender, RoutedEventArgs e) { DispatcherTimer timer1 = new DispatcherTimer(); timer1.Interval = TimeSpan.FromMilliseconds(1); timer1.Tick += timer1_Tick; timer1.Start(); } } }
I dont know why this doesn't work a second has 1000 milliseconds in it and if this timer worked it should
increase the seconds variable every 100 milliseconds but it doesn't it takes so long I stop waiting if I switch the
Interval to TimeSpan.FromSeconds(1) it works fine I've used practically the same code in WinForms and it works fine.