Reputation: 13592
In brief: I want to load my form, then using a Background Worker
fetch data from Database
and set the DataSource of some comboboxes.
In DoWork
event I fetch data, and in RunWorkerCompleted
event I set the datasources. The problem is cross-threading issues. What can I do to resolve this?
I don't understand why setting DisplayMember is OK but in next line, setting ValueMember throws an exception
Upvotes: 0
Views: 4815
Reputation: 458
You can use the control's 'Invoke' method to run code in the same context(thread) as that control. Here is a simple example:
comboBox1.Invoke((MethodInvoker)delegate{
//Code to modify control will go here
comboBox1.Text = "";
});
Upvotes: 11
Reputation: 35477
I think you got it backwards. The DoWork should be FetchData
and DoWorkCompleted should be BindComboBoxes
.
Upvotes: 2