STAThread属性状态 [英] STAThread Attribute Status

查看:116
本文介绍了STAThread属性状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我得到了另一个奇怪的错误,我希望我能得到一些帮助。错误是一个ThreadStateException错误。



System.Threading.ThreadStateException未被用户代码处理

消息=当前线程必须设置为单个可以在进行OLE调用之前进行线程单元(STA)模式。确保您的Main函数上标有STAThreadAttribute。



我要做的是让后台工作者完成并在隐藏原始表单后加载新表单。在出现此错误之前,它会降低设计器生成的代码的一半。下面是错误出现位置的一些背景。



Hi everyone!

I am getting another strange error that I'm hoping I could get some assistance on. The error is a ThreadStateException error.

System.Threading.ThreadStateException was unhandled by user code
Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

What I'm trying to do is let a background worker complete and load the new form after hiding the original one. It gets halfway down the designer generated code before this error appears. Below is some context of where the error appears.

this.transmissionCodeComboBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
            this.transmissionCodeComboBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
            this.transmissionCodeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.transmissionCodeComboBox.Enabled = false;
            this.transmissionCodeComboBox.FormattingEnabled = true;
            this.transmissionCodeComboBox.Location = new System.Drawing.Point(271, 309);
            this.transmissionCodeComboBox.Name = "transmissionCodeComboBox";
            this.transmissionCodeComboBox.Size = new System.Drawing.Size(121, 21);
            this.transmissionCodeComboBox.TabIndex = 65;
            this.transmissionCodeComboBox.SelectedIndexChanged += new System.EventHandler(this.transmissionCodeComboBox_SelectedIndexChanged);





此行失败。

this.transmissionCodeComboBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;



有谁知道如何解决这个问题?



谢谢大家!



It is failing on this line.
this.transmissionCodeComboBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

Does anyone know how this issue can be resolved?

Thanks everyone!

推荐答案

这是一个非常常见的情况,但你可以控制你的线程公寓状态。它实际上是以不同的方式完成的。当您需要设置main(入口点)线程时,可以使用此属性。对于所有其他线程,您可以在其他线程中设置它,在线程启动之前



您将找到所需的详细信息我过去的答案:

调用线程必须是STA,因为许多UI组件在wpf中导航时需要这样做 [ ^ ],

Exception System.Threading.ThreadStateEx C#.. [ ^ ],

设置公寓状态时的示例可能需要:语音识别系统 [ ^ ],

线程和MTA STA [ ^ ],

WPF应用程序异常:调用线程无法访问此对象,因为另一个线程拥有它 [ ^ ]。



-SA
This is quite a usual situation, but you can control your thread apartment state. It actually are done in different ways. You use this attribute when you need to setup you main (entry-point) thread. For all other threads, you can set it in other thread, before your thread is started.

You will find the detail you need in my past answers:
The calling thread must be STA, because many UI components require this while navigation in wpf[^],
Exception System.Threading.ThreadStateEx C#..[^],
example when setting apartment state can be needed: Voice Recognition System[^],
Threading And MTA STA[^],
WPF Application Exception: The calling thread cannot access this object because a different thread owns it[^].

—SA


这篇关于STAThread属性状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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