我正在開發一個應用程序,該應用程序應該通過套接字接口接收命令,然後在GUI中執行它們。此應用程序正在C#.NET 4.0中開發,它使用WPF作爲其GUI。C#GUI線程錯誤
套接字接口有一個工作線程,它不斷偵聽套接字並處理它的命令,所以如果收到一個Show Popup命令,工作線程調用一個負責創建彈出窗口並顯示它的管理器類在主屏幕上。
創建彈出,然後調用主屏幕經理方法如下:
public void ProcessPopup(PopupModel model)
{
switch (model.ScreenType)
{
case Screens.Type1:
popup = new PopupType1();
break;
case Screens.Type2:
popup = new PopupType2();
break;
case Screens.Type3:
popup = new PopupType3();
break;
case Screens.Type4:
popup = new PopupType4();
break;
}
viewModel.SetModel(model);
if (!Dispatcher.CurrentDispatcher.Equals(App.Current.Dispatcher))
{
App.Current.Dispatcher.Invoke((ThreadStart)delegate { mainScreen.ShowPopup(popup); });
}
else
{
mainScreen.ShowPopup(popup);
}
}
類PopupType1是:
public partial class PopupType1 : UserControl
{
public PopupType1()
{
InitializeComponent();
}
}
的問題是,當我瞭解創建一個新的PopupType1對象我得到以下例外:
System.InvalidOperationException: The calling thread must be STA, because many UI components require this.
at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.InputManager.get_Current()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Controls.UserControl..ctor()
at MyApp.Views.PopupType1..ctor()
at MyApp.Manager.ProcessPopup(PopupModel model)
at MyApp.CommunicationController.ProcessAsync(XDocument messageXml)
我試過幾件事我們已經像將我的工作線程轉換爲STA線程,或者創建一個新的STA線程來處理Popup的創建,但是它們引發了比解決問題更多的問題。
最後,我提到我這樣做是很重要的,因爲我的應用程序在其運行過程中遇到了幾個「凍結」,我相信它們與WPF GUI線程太忙不過來的任務有關正確響應,因此我試圖從GUI線程分離非GUI處理。