How to read combobox from a thread other than the thread it was created on?

前端 未结 4 2017
眼角桃花
眼角桃花 2020-12-25 11:43

I am trying to read a combobox.Text from a thread other than the thread it was created on but I am getting the error:

An unhandled exception of type

4条回答
  •  失恋的感觉
    2020-12-25 12:31

    You can still use Invoke and read it to a local variable.

    Something like this:

    string text;
    
    this.Invoke(new MethodInvoker(delegate() { text = combobox.Text; }));
    

    Since Invoke is synchronous you have the guarantee that text variable will contain the value of the combo box text after it returns.

提交回复
热议问题