Tous les contrôles ont une méthode appelée Invoke, qui prend un délégué comme premier argument et optionnel params object [].
Vous pouvez utiliser cette méthode facilement:
richTextBox1.Invoke(new MethodInvoker(DoSomething));
où
void DoSomething()
{
richTextBox1.BackColor = Color.Cyan;
}
Le délégué MethodInvoker est dans l'espace de noms System.Windows.Forms, qui, je suppose, que vous utilisez déjà.
Vous pouvez même invoquer à partir du même fil!
Vous pouvez également utiliser des paramètres, comme ceci:
richTextBox1.Invoke(new ColorChanger(DoSomething), Color.Cyan);
où
delegate void ColorChanger(Color c);
void DoSomething(Color c)
{
richTextBox1.BackColor = c;
}
J'espère que cela a aidé!
Modifier:
InvokeRequired
est requis si vous utilisez la même méthode à partir d'un thread ... fondamentalement ... inconnu. Il ressemblerait à ceci:
void DoSomething()
{
if (richTextBox1.InvokeRequired)
richTextBox1.Invoke(new MethodInvoker(DoSomething));
else
{
richTextBox1.BackColor = Color.Cyan;
// Here should go everything the method will do.
}
}
Vous pouvez appeler cette méthode à partir de fil!
Et pour les paramètres:
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.
}
}
Profitez de programmation!
ou Invoke BeginInvoke – CodesInChaos
double possible de [l'aide multithreading, encore une fois (Winforms)] (http://stackoverflow.com/questions/3999020/multithreading-help-yet-again-winforms) –