gpt4 book ai didi

c# - 单线程单元问题

转载 作者:太空狗 更新时间:2023-10-29 19:39:33 25 4
gpt4 key购买 nike

在我的主窗体中,我调用以下命令打开一个新窗体

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

一切都很好,但是这个表单有一个组合框,当我将其 AutoCompleteMode 切换为建议和附加时,我在显示表单时遇到了这个异常:

在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。确保您的 Main 函数上标记了 STAThreadAttribute。

我已经按照异常的要求在我的主函数上设置了这个属性:

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

我能得到一些帮助来了解可能出了什么问题吗。

示例代码:

private void mainFormButtonCLick (object sender, EventArgs e)
{
// System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
MyForm form = new 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 = 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";

设置数据源

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

最佳答案

Main(string[] args) 真的是您的入口点吗?

也许您有另一个没有参数的 Main() 重载。或者另一个类中的其他 Main() 。请打开项目属性并查找起始对象。

关于c# - 单线程单元问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13580475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com