Voici le code qui jette un InvalidOperationExecption Chaque fois que j'essaie de texte à My Label ... Programmaticallycode Lancer InvalidOperationException Tout réglage Texte Étiqueter Programmatically
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Project
{
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
Thread quit = new Thread(Quit);
Thread Load = new Thread(LoadIt);
Load.Start();
quit.Start();
}
private void Splash_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
public static void Quit()
{
Thread.Sleep(3000);
Application.Exit();
}
public void LoadIt()
{
Thread.Sleep(500);
Loading.Text = "Loading..";
}
}
}
Pourquoi ce code va lancer une exception? Quel est le problème?
"Chargement" est le nom de My Label
Parce que vous avez écrit « Loading .. » avec seulement deux points. –
Assurez-vous que vous êtes sur le thread principal, n'appelant pas à partir d'un thread d'arrière-plan ou d'une tâche asynchrone. Les contrôles ne peuvent obtenir leurs propriétés définies que sur le thread principal, et c'est généralement la raison pour laquelle vous obtenez cette exception. –
Les «informations supplémentaires» vous donne la raison - vous ne pouvez pas définir la propriété d'un contrôle d'interface utilisateur à partir d'un thread différent. –