简体   繁体   中英

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

在此处输入图片说明

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

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 = "";
        });   

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM