As others have said, the problem is that you are blocking the user interface thread.
, Application.DoEvents, DispatcherTimer . :
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += delegate
{
label.Content = counter.ToString();
counter++;
if (counter == 500)
{
timer.Stop();
}
};
timer.Interval = TimeSpan.FromMilliseconds(2000);
timer.Start();
( , Application.DoEvents WPF, . , , . .)
Timer ( System.Threading.Timer, System.Timers.Timer), . DispatcherTimer - Tick Dispatcher.
EDIT: DoEvents, , , MSDN docs Dispatcher.PushFrame, , Application.DoEvents, WPF:
public void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrames), frame);
Dispatcher.PushFrame(frame);
}
public object ExitFrames(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 500; i++)
{
label.Content = i.ToString();
DoEvents();
}
}
- , WPF (, Windows Forms) , .