I am calling a bunch of methods asynchronously. Somehow the release version works fine but while debugging await task1;
gives me an exception:
"System.InvalidOperationException: 'Cross-thread operation not valid: Control 'comboBox1' accessed from a thread other than the thread it was created on"
on line comboBox1.ValueMember = "value member";
async private void CallingMethod()
{
Task task1 = Task.Factory.StartNew(TroubleMethod);
Task task2 = Task.Factory.StartNew(Method1);
var task3 = Task.Factory.StartNew(() => Method2(Param1));
//other tasks
//other stuff
await task3;
await task2;
await task1;
}
private void TroubleMethod()
{
class1 thedata = getDBdata();
comboBox1.DataSource = thedata;
comboBox1.DisplayMember = "display member";
comboBox1.ValueMember = "value member";
class2 thedata2 = getDBdata2();
comboBox2.DataSource = thedata2;
comboBox2.DisplayMember = "display member2";
comboBox2.ValueMember = "value member2";
}
TroubleMethod()
has only one reference which is task1
. So why the exception?