C#GUI线程错误 [英] C# GUI Thread Error

查看:132
本文介绍了C#GUI线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应通过套接字接口接收命令,然后在GUI中执行命令的应用程序。此应用程序是使用C#.NET 4.0开发的,它使用WPF作为其GUI。

I am developing an application that should receive commands through a socket interface and then execute them in a GUI. This application is being developed in C# .NET 4.0 and it uses WPF for its GUI.

套接字接口具有一个工作线程,该线程不断侦听套接字并处理其命令。 ,因此,例如,如果收到显示弹出窗口命令,则工作线程会调用一个管理器类,该类负责创建弹出窗口并将其显示在主屏幕上。

The socket interface has a worker thread that keeps listening to the socket and processing its commands, so if for example a Show Popup command is received, the worker thread calls a manager class that is responsible for creating the popup and showing it on the main screen.

创建弹出窗口然后调用主屏幕的管理器方法如下:

The manager method that creates the popup and then calls the main screen is the following:

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类为:

The class PopupType1 is:

public partial class PopupType1 : UserControl
{
    public PopupType1 ()
    {
        InitializeComponent();
    }
}

问题是当我创建新的PopupType1对象时我收到以下异常:

The problem is that when I craete a new PopupType1 object I get the following exception:

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的创建,但是它们引起的问题比解决的问题多。

I've tried several things already like transforming my worker thread into a STA thread, or creating a new STA thread just to handle the creation of the Popup, but they caused more problems than they solved.

最后,提到我正在这样做,因为我的应用程序在运行过程中遇到了几次冻结,并且我相信它们与以下事实有关:WPF GUI线程太忙于无法正确响应的任务,因此我尝试将其分离

Finally it is important to mention that I am doing this, because my application is experiencing several "freezes" during its operation and I believe that they are related to the fact that the WPF GUI thread is too overwhelmed with tasks to be properly responsive, therefore I am trying to separate the non-GUI processing from the GUI thread.

推荐答案

您还需要在UI线程上创建UI控件。因此,基本上所有 ProcessPopup 都需要在您的情况下在UI线程上执行,而不仅仅是 mainScreen.ShowPopup ()

You need to create the UI control on the UI thread as well. So basically all of ProcessPopup needs to be executed on the UI thread in your case and not just mainScreen.ShowPopup()

这篇关于C#GUI线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆