WPF - 调度程序不能按预期工作 - 减慢速度

时间:2016-01-25 19:09:30

标签: c# wpf multithreading xaml dispatcher

我遇到了更新Wpf-Application窗口的问题。谷歌搜索了很长一段时间后,我把问题分解为下面的例子。我希望你能帮助我。

假设我们有一个带有Button和TextBlock的窗口......没什么特别的:

...
<Grid>
  <Button ... Click="button_Click"/>
  <TextBox x:Name="textBox" Text="DefaultText" .../>
</Grid>
...

button_Click是一个简单的事件:

private void button_Click(object sender, RoutedEventArgs e)
{
  for (int i = 0; i < 20; i++)
  {
    textBox.Dispatcher.Invoke(new Action(() => textBox.Text = i.ToString());
  }
}

这是我的问题。如果我调试循环,我的TextBlock的文本始终是“DefaultText”。只有在循环结束时,Text才是“19”。为什么我的调度员无法更改文本?就像这里描述的那样(What's the difference between Invoke() and BeginInvoke()),Dispatcher应该停止我的线程,直到工作完成。 我尝试了不同的DispatcherPriorities ......但没有区别。我的问题是什么?

感谢您帮助我。

0 个答案:

没有答案