单线程单元问题 [英] Single thread apartment issue

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

问题描述

从我的MainForm我调用下面打开一个新的形式

  MyForm的某物=新建MyForm(); 
sth.show();



一切的伟大工程,但是这种形式有一个组合框,当我将它的AutoCompleteMode建议和追加,我得到这个例外,同时显示的形式:



当前线程必须设置为单线程单元(STA)模式下可以进行OLE调用之前。确保您的主要功能有请将STAThreadAttribute上标注



我已经设置我的主要功能这个属性由异常的要求:

  [STAThread] 
静态无效的主要(字串[] args)
{...

我能请得到一些帮助,了解什么可能是错误的。



示例代码:

 私人无效mainFormButtonCLick(对象发件人,EventArgs五)
{
//系统.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
MyForm的形式=新建MyForm();
form.show();
}



设计师:

  this.myCombo.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; 
this.myCombo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.myCombo.FormattingEnabled = TRUE;
this.myCombo.Location =新System.Drawing.Point(20,12);
this.myCombo.Margin =新System.Windows.Forms.Padding(4,5,4,5);
this.myCombo.Name =myCombo;
this.myCombo.Size =新System.Drawing.Size(430,28);
this.myCombo.Sorted = TRUE;
this.myCombo.TabIndex = 0;那句;



设置数据源

 公共MyForm的(名单<串> ELEM)
{
的InitializeComponent();
populateColorsComboBox();
PopulateComboBox(ELEM);
}

公共无效PopulateComboBox(列表<串GT;清单)
{
this.myCombo.DataSource = NULL;
this.myCombo.DisplayMember =TEXT ;
this.myCombo.DataSource =清单;
}


解决方案

主要(字串[] args)真的是你的切入点?



也许你有不带参数的另一个main()的过载,或者一些其他的Main()中的另一个类,请打开项目属性,并寻找起始对象。


From my mainform I call the following to open a new form

MyForm sth = new MyForm();
sth.show();

Everything works great however this form has a combobox that, when I switch its AutoCompleteMode to suggest and append, I got this exception while showing the form:

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.

I have set this attribute on my main function as requested by the exception:

[STAThread]
static void Main(string[] args)
{ ...

Can I please get some help as to understand what might be wrong.

Sample code:

private void mainFormButtonCLick (object sender, EventArgs e)
{
    // System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
    MyForm form = new MyForm();
    form.show();
}

Designer:

this.myCombo.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.myCombo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.myCombo.FormattingEnabled = true;
this.myCombo.Location = new System.Drawing.Point(20, 12);
this.myCombo.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.myCombo.Name = "myCombo";
this.myCombo.Size = new System.Drawing.Size(430, 28);
this.myCombo.Sorted = true;
this.myCombo.TabIndex = 0; phrase";

Setting data source

public MyForm(List<string> elem)
{
    InitializeComponent();
    populateColorsComboBox();
    PopulateComboBox(elem);
}

public void PopulateComboBox(List<string> list )
{
    this.myCombo.DataSource = null;
    this.myCombo.DisplayMember = "text";
    this.myCombo.DataSource = list;
}

解决方案

Is Main(string[] args) really your entry point?

Maybe you have another Main() overload with no parameters. Or some other Main() in another class. Please open Project properties and look for the start object.

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

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