// Les opérations inter-thread ne sont pas valides ======= NG
private void button1_Click(object sender, EventArgs e)
{ Thread t = new Thread(RunWorkerThread); t.Start(); }
void RunWorkerThread(Object o)
{ SetBackground(null); }
void SetBackground(Object o)
{ //this.BackColor = Brushes.Red ;
this.BackColor = System.Drawing.SystemColors.ButtonFace ;
}
//Les opérations inter-thread sont valides =======OK
private void button2_Click(object sender, EventArgs e)
{ Thread t = new Thread(RunWorkerThread11); t.Start(SynchronizationContext.Current);//Synchronisation actuelle entrante Context } void RunWorkerThread11(Object o) { //Utiliser le contexte entrant pour envoyer le message au thread d'origine SynchronizationContext sc = o as SynchronizationContext ; sc.Post(SetBackground11, null); }
void SetBackground11(Object o)
{ this. BackColor = System.Drawing.SystemColors.ActiveCaptionText ; }