【发布时间】:2014-02-04 22:59:30
【问题描述】:
我有一个 WPF 窗口,它在其构造函数中创建并启动一个计时器。计时器已用事件触发一个方法 (SyncPTUpdate),该方法使用 BeginInvoke 将另一个方法 (PTProgressUpdateInThread) 调用到 Window 的线程上。然后这会异步调用 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
//...
}
【问题讨论】:
-
顺便说一句,使用
System.Action代表。 -
你为什么要在 UI 线程上运行?
-
你的意思是我为什么要在 Window 线程上运行计时器?好点,只是因为我来自单线程文化,只是慢慢地掌握了多线程。我认为一个新的计时器无论如何都会产生一个新线程?
-
如果你想让你的定时器在主线程上触发,你可以使用
System.Windows.Threading.DispatcherTimer。 -
听起来UI卡死可能是你的cmets造成的。
// now use the results of the WCF call to update the Window UI
标签: c# wpf multithreading wcf tap