C#跨线程操作在BackgroundWorker中无效

加载主表单时,我正在向列表框添加项目:

private void MainForm_Load(object sender, EventArgs e)
    {


        Dictionary<string, string> item = new Dictionary<string, string>();
        item.Add("Test 1", "test1");
        item.Add("Test 2", "test 2");

        cmbTest.DataSource = new BindingSource(item, null);
        cmbTest.DisplayMember = "Key";
        cmbTest.ValueMember = "Value";


    }

然后,我尝试在BackgroundWorker中获取选定的项目值,但失败。

private void TestWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string test = ((KeyValuePair<string, string>)cmbTest.SelectedItem).Value;
        MessageBox.Show(test);
    }
评论