![](/img/trans.png)
[英]Dispatcher.CurrentDispatcher.BeginInvoke Not Invoking
[英]Why using Dispatcher.CurrentDispatcher.BeginInvoke does not update my GUI but using BeginInvoke does?
我对Dispatcher.CurrentDispatcher.BeginInvoke和BeginInvoke之间的区别感到困惑
我有以下部分代码无效,UpdateCounts方法中的代码被忽略:
private void Start()
{
_testResults = new TestResults(ModelNameTextBox.Text);
_timer = new System.Threading.Timer(UpdateCounts, null, 0, 500);
}
private void UpdateCounts(object info)
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
PassCountLabel.Text = _testResults.PassedCount.ToString();
RequestCountLabel.Text = _testResults.RequestedCount.ToString();
}));
}
但是一旦删除Dispatcher.CurrentDispatcher,它就可以正常工作:
private void UpdateCounts(object info)
{
BeginInvoke(new Action(() =>
{
PassCountLabel.Text = _testResults.PassedCount.ToString();
RequestCountLabel.Text = _testResults.RequestedCount.ToString();
}));
}
如果你从UI线程调用它 的 Dispatcher.CurrentDispatcher.BeginInvoke 只会工作,如果没有它调用当前线程。
您必须使用 Application.Current.Dispatcher 来调用UI线程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.