I'm trying to update the text of some GUI controls from a second thread but Visual Studio shows an exception:
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional information:
Invalid operation through sub-processes: access to 'impPROF_combo' took control from a other thread than that in which he created .
My code is:
private async void checkBox1_CheckedChanged(object sender, EventArgs e)
{
sciLoadingCircle1.Visible = true;
sciLoadingCircle1.Active = true;
await Task.Run(() => refreshComboColumnsNames());
sciLoadingCircle1.Visible = false;
sciLoadingCircle1.Active = false;
}
private void refreshComboColumnsNames()
{
object[] columnsNames = this.generateComboColumnsNames();
int impPROF_combo_selected = impPROF_combo.SelectedIndex; //the exceptions throws from here
impPROF_combo.Items.Clear();
impPROF_combo.Items.AddRange(columnsNames);
impPROF_combo.SelectedIndex = impPROF_combo_selected;
}
How I can do this the right way? Thank you.