Pourquoi cela ne fonctionne-t-il pas?TextBox et Thread
Le programme arrête sur: this.textBox1.Text = "(New text)";
Thread demoThread;
private void Form1_Load(object sender, EventArgs e)
{
this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
textBox1.Text = "Written by the main thread.";
}
private void ThreadProcUnsafe()
{
while (true)
{
Thread.Sleep(2000);
this.textBox1.Text = "(New text)";
}
}
Vous pouvez utiliser la méthode this.InvokeRequired pour voir si le thread en cours d'exécution est le thread de l'interface utilisateur. Si .InvokeRequired est faux, vous pouvez mettre à jour en toute sécurité la propriété .Text du contrôle. – TLiebe
Absolument - mais dans son exemple, il est nécessaire. –