"调用线程必须STA,因为许多UI组件都需要这个&QUOT。 WPF [英] "The calling thread must be STA, because many UI components require this." WPF

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

问题描述

我遇到的消息一个InvalidOperationException调用线程必须STA,因为许多UI组件都需要这个。在WPF应用程序上引用库的严重依赖。

我试图拖住其中,误差是从哪里来的,使用不同的线程和对象的调度,确保主()有STAthread属性,尝试设置[STAThread]在表面上相关的方法。

里面MyParticipant构造函数,如MyVideoRenderer PIC正在建设中,继承VideoRenderer的VideoRenderer构造本身是抛出此异常,不进入的构造。

code:

 公共类MyParticipant:参与者//内部MainWindow.xaml.cs
    {
        公共枚举PictureMode
        {
            阿凡达
            视频
        }

        公共PictureMode pictureMode = PictureMode.Avatar;

        公共进度voiceVolume;
        公共标签nameLabel;
        公共MyVideoRenderer知情同意;
        公共MyVideo网站的视频;

        公共BOOL isCachedInClient = FALSE;
        公共字符串显示名= NULL;
        公众形象avatarImage = NULL;

        公共静态INT picHeight = 480;
        公共静态INT piclWidth = 640;
        公共静态INT panelHeight = 155;
        公共静态INT panelWidth = 174;

        公共静态颜色liveColor = SystemColors.GradientActiveCaptionColor;
        公共静态颜色nonLiveColor = SystemColors.GradientInactiveCaptionColor;


        公共MyParticipant(UINT OBJECTID,视频管理视频管理)
            :基地(OBJECTID,视频管理)
        {
            PIC =新MyVideoRenderer(videoManagerRef)
            {
                //顶= 5,
                //左= 5,
                身高= picHeight,
                宽度= piclWidth,
                // SizeMode = PictureBoxSizeMode.StretchImage
            };
...

公共类VideoRenderer:System.Windows.Controls.Image //引用外部类
{
    公共VideoRenderer(VideoManagerRoot视频管理)///异常这里
    {
        this.videoManagerRef =视频管理;
    }
...
 

解决方案

解决了,感谢拉法尔的帖子:

问题是,这是建立一个新的MyParticipant的线程被默认设置为MTA,所以里面MyParticipant,该MTA线程调用new VideoRenderer,它继承了图片。 MTA线程构建UI控件导致此异常。

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.

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.

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

Code:

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;
    }
...

解决方案

Solved, thanks to Rafal's post:

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组件都需要这个&QUOT。 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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