Tovarășe Alexandru,
All controls have a method called Invoke, which takes a delegate as the first argument and optional params object[].
You can use this method easily:
richTextBox1.Invoke(new MethodInvoker(DoSomething));
where
void DoSomething()
{
richTextBox1.BackColor = Color.Cyan;
}
The delegate MethodInvoker is in the System.Windows.Forms namespace, which, I suppose, you are already using.
You can even invoke from the same thread!
You can also use parameters, like this:
richTextBox1.Invoke(new ColorChanger(DoSomething), Color.Cyan);
where
delegate void ColorChanger(Color c);
void DoSomething(Color c)
{
richTextBox1.BackColor = c;
}
I hope this helped!
Edit:
BeginInvoke
is required if you are using the same method from a... basically... unknown thread.
So it would look like this:
void DoSomething()
{
if (richTextBox1.InvokeRequired)
richTextBox1.Invoke(new MethodInvoker(DoSomething));
else
{
richTextBox1.BackColor = Color.Cyan;
// Here should go everything the method will do.
}
}
You may call this method from ANY thread!
And for parameters:
delegate void ColorChanger(Color c);
void DoSomething(Color c)
{
if (richTextBox1.InvokeRequired)
richTextBox1.Invoke(new ColorChanger(DoSomething), c);
else
{
richTextBox1.BackColor = c;
// Here should go everything the method will do.
}
}
Enjoy programming!