繁体   English   中英

等待Dispatcher.BeginInvoke…(使用WPF和WCF)时,我的UI线程被阻止了

[英]My UI thread is blocked when await Dispatcher.BeginInvoke… (using WPF and WCF)

我有一个WPF窗口,它在其构造函数中创建并启动一个计时器。 计时器经过的事件触发一个方法(SyncPTUpdate),该方法使用BeginInvoke将对另一个方法(PTProgressUpdateInThread)的调用放置到窗口的线程上。 然后,这将异步调用WCF调用(使用TAP模式,该模式由VS 2013自动生成)。

当我人为地长时间进行WCF调用时(在服务器组件中使用thread.sleep),WPF应用程序的UI会冻结。 最初不是,但是几秒钟之后。

我要去哪里错了?

public delegate void PTProgressDelegate();

// this method is called from a periodically firing timer (System.Timers)
private async void SyncPTUpdate(object sender, ElapsedEventArgs e)
{
    await this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new PTProgressDelegate(PTProgressUpdateInThread));
}

private async void PTProgressUpdateInThread()
{
    PTMapFieldClient = new FieldClient();
    ServiceField.BlokPTProgress PTProgressFromServer = await PTMapFieldClient.GetPTProgressAsync(variousparametershere);
    PTMapFieldClient.Close();

    // now use the results of the WCF call to update the Window UI
    //...
}

Dispatcher.BeginInvoke()通常作为一种异步处理方式而呈现,其好处是不必创建/涉及另一个线程。

但这意味着它仅对小型轻量级工作有益。 最好的办法是将对PTProgressUpdateInThread()的调用推送到ThreadPool。 或者使用线程计时器。
你不使用后的结果await反正。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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