4

I need to get the text in a RichTextBox while in another thread. I tried invoking like this:

string text = ResultsRTB.Invoke((MethodInvoker)(() => ResultsRTB.Text));

But that obviously doesnt work due to the fact that you can't return anything with MethodInvoker (that I know of). I also tried variations of the suggestions here and here with no luck. I feel like theres an easy way to do this but Im just missing one little thing. Thanks!

Community
  • 1
  • 1
Hershizer33
  • 1,206
  • 2
  • 23
  • 46

1 Answers1

15

You need a delegate type that returns a string. Like Func<string>:

var text = (string)richTextBox1.Invoke(new Func<string>(() => richTextBox1.Text));
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536