Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13592

Control accessed on a thread other than the thread it was created on

enter image description here

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

Answers (2)

Keagan Ladds
Keagan Ladds

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

Richard Schneider
Richard Schneider

Reputation: 35477

I think you got it backwards. The DoWork should be FetchData and DoWorkCompleted should be BindComboBoxes.

Upvotes: 2

Related Questions