30

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 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'levelsComboBox' accessed from a thread other than the thread it was created on.

I have used .Invoke before but only to set properties, how can I use it to read combobox.Text? Because .Invoke returns void and I need a string. Or is there another way to do it without the Invoke?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
nitrkli
  • 369
  • 1
  • 4
  • 7
  • I think that this is a duplicate of the [How to get return value when BeginInvoke/Invoke is called in C#](http://stackoverflow.com/questions/2214002/how-to-get-return-value-when-begininvoke-invoke-is-called-in-c) – DevExpress Team Apr 01 '11 at 16:30

4 Answers4

52

You can do it like this:

this.Invoke((MethodInvoker)delegate()
    {
        text = combobox.Text;
    });
BrandonZeider
  • 8,014
  • 2
  • 23
  • 20
18

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.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
5

Shortest way is:

string text;
this.Invoke(() => text = combobox.Text);
Igor Vaschuk
  • 2,794
  • 1
  • 19
  • 11
  • This does not seem to work. See link: http://connect.microsoft.com/VisualStudio/feedback/details/395813/system-delegate-is-not-a-delegate-type – Bill Software Engineer Jul 21 '12 at 15:14
  • 1
    @YongkeBillYu the link asks to sign in. I don't see why wouldn't it work though, it should do the same as the accepted solution. – Hi-Angel Apr 14 '16 at 14:05
  • @Igor there's a caveat though: compiler complains on the code unless [you're cast it into Action type](http://stackoverflow.com/questions/411579/why-must-a-lambda-expression-be-cast-when-supplied-as-a-plain-delegate-parameter). Please, edit the code. – Hi-Angel Apr 14 '16 at 14:25
3

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).

Morten Mertner
  • 9,414
  • 4
  • 39
  • 56