<呼叫线程必须是STA,因为许多UI组件都需要STA.WPF [英] "The calling thread must be STA, because many UI components require this." WPF

查看:45
本文介绍了<呼叫线程必须是STA,因为许多UI组件都需要STA.WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一条InvalidOperationException,消息为:调用线程必须是STA,因为许多UI组件都需要STA."在很大程度上依赖于引用库的WPF应用程序中.

I'm encountering an InvalidOperationException with the message "The calling thread must be STA, because many UI components require this." in a WPF application with heavy dependence on a referenced library.

我尝试使用各种线程和对象的分派器来确定错误的出处,确保main()具有STAthread属性,并尝试在看似相关的方法上设置"[STAThread]".

I've tried to pin down where the error is coming from, using dispatchers of various threads and objects, ensured main() has STAthread attribute, tried setting "[STAThread]" on seemingly relevant methods.

在MyParticipant构造函数内部,由于正在构造继承VideoRenderer的MyVideoRenderer图片,因此VideoRenderer构造函数本身会抛出此异常,而不是进入构造函数.

Inside MyParticipant constructor, as MyVideoRenderer pic is being constructed, which inherits VideoRenderer, the VideoRenderer constructor itself is throwing this exception, not entering the constructor.

代码:

public class MyParticipant : Participant           //inside MainWindow.xaml.cs
    {
        public enum PictureMode
        {
            Avatar,
            Video
        }

        public PictureMode pictureMode = PictureMode.Avatar;

        public ProgressBar voiceVolume;
        public Label nameLabel;
        public MyVideoRenderer pic;
        public MyVideo video;

        public bool isCachedInClient = false;   
        public string displayName = null;
        public Image avatarImage = null;

        public static int picHeight = 480;
        public static int piclWidth = 640;
        public static int panelHeight = 155;
        public static int panelWidth = 174;

        public static Color liveColor = SystemColors.GradientActiveCaptionColor;
        public static Color nonLiveColor = SystemColors.GradientInactiveCaptionColor;


        public MyParticipant(uint objectId, VideoManager videoManager)
            : base(objectId, videoManager)
        {
            pic = new MyVideoRenderer(videoManagerRef)   
            {
                //Top = 5,
                //Left = 5,
                Height = picHeight,
                Width = piclWidth,
                //SizeMode = PictureBoxSizeMode.StretchImage
            };
...

public class VideoRenderer : System.Windows.Controls.Image         //referenced external class
{
    public VideoRenderer(VideoManagerRoot videoManager)        ///Exception here
    {
        this.videoManagerRef = videoManager;
    }
...

推荐答案

已解决,多亏了拉法尔(Rafal)的帖子:

Solved, thanks to Rafal's post:

问题在于,正在创建新MyParticipant的线程被默认设置为MTA,因此在MyParticipant内部,MTA线程正在调用新的VideoRenderer,该继承了Image.构造UI控件的MTA线程会导致此异常.

The problem is that the thread that was creating a new MyParticipant was being default set to MTA, and so inside MyParticipant, that MTA thread was calling new VideoRenderer, which inherits an Image. An MTA thread constructing a UI control results in this exception.

这篇关于<呼叫线程必须是STA,因为许多UI组件都需要STA.WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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