Pet*_*ter 50 wpf multithreading dispatcher
Dispatcher当你没有引用任何UI元素时,有没有办法获得UI线程?
Abe*_*cht 94
您可以从静态应用程序实例中获取UI Dispatcher: Application.Current.Dispatcher
您可能希望首先检查Application.Current空值,因为它可以在关闭序列期间清除.
小智 7
当在碰巧也使用 WPF 的 WinForms 应用程序中使用时,以下对我来说效果更好(在我的情况下是 Esri 的 Arcmap.exe)
private System.Windows.Threading.Dispatcher Dispatcher { get; set; }
// I put this in my view model constructor as it MUST be called from UI thread
Dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
Run Code Online (Sandbox Code Playgroud)
每次我打开一个新的 WPF 窗口然后关闭它时,另一种方法(涉及调用new System.Windows.Application()populate System.Windows.Application.Current)都会给我带来问题。这不仅 null out System.Windows.Application.Current,而且我无法再打开新窗口作为他们的InitializeComponents()方法,都失败了:
System.InvalidOperationException: '应用程序对象正在关闭。'
到目前为止,新的解决方案没有这些副作用。