Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the text in a RichTextBox that's on another thread

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!

like image 742
Hershizer33 Avatar asked Dec 06 '22 13:12

Hershizer33


1 Answers

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

var text = (string)richTextBox1.Invoke(new Func<string>(() => richTextBox1.Text));
like image 173
Hans Passant Avatar answered Mar 27 '23 04:03

Hans Passant