I hit a problem and I thought it might be due to the complexity of my classes passing objects to each other so I minimised it and the problem persists:
I've got a default winform project created in VS2017 Community
On the form I've added a textbox, a richtextbox, a backgroundworker and a button to activate the background worker.
I've put the following code in the form to populate the text boxes and to run the worker on button click:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "Hello";
richTextBox1.Text = "World!";
}
private void button1_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
MessageBox.Show(textBox1.Text);
MessageBox.Show(richTextBox1.Text);
}
}
I run the program and I don't understand what happens next.
textBox1.Text
is accessible from the form so the MessageBox
shows fine. richTextBox1.Text
is NOT accessible and gives me this error:
Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.
WHY?
I'm assuming richTextBox has more routing and wrapping but is the .Text
property not exactly the same?! What's going on here?
EDIT: I don't think this is a duplicate to the marked question because his wasn't working for TextBox.Text
whereas mine is. I'm asking about the DIFFERENCE BETWEEN TextBox and RichTextBox .Text properties.