2

The error I'm having from my thread is:

Cross-thread operation not valid. Control 'richTextBox8' accessed from a thread other than the thread it was created on.

I have this code I use for my List of string that is causing the error.

string[] parts = richTextBox8.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

Now I'm working with using System.Threading that requires to transform the code above into a format something like this code to work but I'm unable to do it or is there some other way?

richTextBox8.Invoke((Action)(() => richTextBox8.Text += "http://elibrary.judiciary.gov.ph/" + str + "\n"));
5
  • 4
    Your second code sample (or something very much like it) is what you need. What problem are you getting with the second code?
    – ChrisF
    Apr 13, 2012 at 12:18
  • What typeof is your textbox ? I've read that some 3rd Party vendors have problems in this scenario Apr 13, 2012 at 12:20
  • Ideally, you should grab richTextBox8.Text before you start the threaded operation - could you give us more context please?
    – Jon Skeet
    Apr 13, 2012 at 12:21
  • Hi. The second code is working properly. I just want to know how to format the first code just like the second code.
    – guitarPH
    Apr 13, 2012 at 12:22
  • 2
    richTextBox8.Invoke((Action)(() => { /*do whatever you want */}));
    – L.B
    Apr 13, 2012 at 12:24

3 Answers 3

2

your string array (string[]) looks fine to me. If there are whitespaces inisde richTextBox8 it should do the splitting.

Regarding your Threading, try with a use of delegate, like:

    public delegate void MyDelegate(string message);

   //when you have to use Invoke method, call this one:
   private void UpdatingRTB(string str)
   {
       if(richTextBox8.InvokeRequired)
           richTextBox8.Invoke(new MyDelegate(UpdatingRTB), new object []{ msg });
       else
           richTextBox8.AppendText(msg);
   }
1
string[] parts = null;
richTextBox8.Invoke((Action)(() => 
    {
        parts = richTextBox8.Text.Split(new string[] { " " },
        StringSplitOptions.RemoveEmptyEntries); //added semicolon
    }));
0
1

You only need the text extraction done on the UI thread.

With variable capturing:

string text = null;
richTextBox8.Invoke((Action)(() => text = richTextBox8.Text));
string[] parts = text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

Without variable capturing (slightly more efficient):

var ret = (string)richTextBox8.Invoke((Func<string>)(() => richTextBox8.Text));
parts = ret.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
1
  • You're welcome. Note that this is slightly more efficient than the answer you selected, because you don't do the splitting on the UI thread (as a rule, you want to stay out of the UI thread as much as you can) Apr 13, 2012 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.