繁体   English   中英

为什么使用Dispatcher.CurrentDispatcher.BeginInvoke不会更新我的GUI,而使用BeginInvoke呢?

[英]Why using Dispatcher.CurrentDispatcher.BeginInvoke does not update my GUI but using BeginInvoke does?

我对Dispatcher.CurrentDispatcher.BeginInvokeBeginInvoke之间的区别感到困惑

我有以下部分代码无效,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM