La mise à jour du thread d'interface utilisateur de l'opération de sous-thread C # signale que l'opération inter-thread n'est pas valide

 // 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 ;         }

Guess you like

Origin blog.csdn.net/u014090257/article/details/127683090