san*_*art 8 c# multithreading thread-safety winforms
从我的mainform我打电话给以下打开一个新表格
MyForm sth = new MyForm();
sth.show();
Run Code Online (Sandbox Code Playgroud)
一切都很好但是这个表单有一个组合框,当我将其AutoCompleteMode切换为建议和附加时,我在显示表单时遇到了这个异常:
在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式.确保您的Main函数标记了STAThreadAttribute.
我已根据异常的请求在我的main函数上设置了此属性:
[STAThread]
static void Main(string[] args)
{ ...
Run Code Online (Sandbox Code Playgroud)
我可以请一些帮助来了解可能出错的地方.
示例代码:
private void mainFormButtonCLick (object sender, EventArgs e)
{
// System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
MyForm form = new MyForm();
form.show();
}
Run Code Online (Sandbox Code Playgroud)
设计师:
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";
Run Code Online (Sandbox Code Playgroud)
设置数据源
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;
}
Run Code Online (Sandbox Code Playgroud)