Right now, you keep invoking a method on the dispatcher thread that loops from 0 to 1000, and it blocks the dispatcher thread 100 milliseconds while it is in that loop. You should not be blocking the dispatcher thread, only block your loop thread. Also, I think you probably want an
if
instead of a
while
.
Try this:
private void loop()
{
if (check)
{
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(100);
Dispatcher.Invoke(() =>
{
txtResult.Text = i.ToString();
}
}
}
}