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

五迷三道 提交于 2019-11-30 01:30:47

You can do it like this:

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

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.

Shortest way is:

string text;
this.Invoke(() => text = combobox.Text);

The easiest solution is to use the BackgroundWorker class to execute work on another thread, while still being able to update the UI (e.g. when reporting progress or when the task has completed).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!