您好我有以下代码 将一些项目添加到组合框中当我在线程中执行代码时我有以下异常“特定强制转换无效”但何时在主窗体中执行此操作也不例外。 任何想法如何解决这个问题?
Control.CheckForIllegalCrossThreadCalls = false;
//Auto Complete
comboBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox3.AutoCompleteSource = AutoCompleteSource.ListItems;
//no specific formatting
comboBox3.FormattingEnabled = true;
Thread th1 = new Thread(() =>
{
try
{
//# Code Start
//Some Text
String[] TicketStatus = new string[] { "a", "b", "c", "d", "e" };
//Throw Exception "Specific cast is not valid"
comboBox3.Items.AddRange(TicketStatus);
//# Code End
}
catch (Exception c) { MessageBox.Show(c.Message); }
});
th1.Start();
像这样没有例外
Control.CheckForIllegalCrossThreadCalls = false;
//Auto Complete
comboBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox3.AutoCompleteSource = AutoCompleteSource.ListItems;
//no specific formatting
comboBox3.FormattingEnabled = true;
//# Code Start
//Some Text
String[] TicketStatus = new string[] { "a", "b", "c", "d", "e" };
//Throw Exception "Specific cast is not valid"
comboBox3.Items.AddRange(TicketStatus);
//# Code End
答案 0 :(得分:1)
您无法从UI线程以外的线程访问UI元素。
您必须将setter代码编组回UI线程。
使用InvokeRequired
来执行此操作。