La actualización del subproceso de la interfaz de usuario de la operación del subproceso de C# informa que la operación entre subprocesos no es válida

 //Las operaciones entre subprocesos no son válidas =======NG
        private void button1_Click(object sender, EventArgs e)
        {             Thread t = new Thread(RunWorkerThread);             t.Start();         }


         void RunWorkerThread(Objeto o)
        {             SetBackground(null);         }

         void SetBackground(Object o)
        {             //this.BackColor = Brushes.Red;

            this.BackColor = System.Drawing.SystemColors.ButtonFace;
        }


        //Las operaciones entre hilos son válidas =======OK
        private void button2_Click(object sender, EventArgs e)
        {             Thread t = new Thread(RunWorkerThread11);             t.Start(SynchronizationContext.Current);//Sincronización actual entrante Contexto         }          void RunWorkerThread11(Object o)         {             //Usar el contexto entrante para enviar el mensaje al subproceso original             SynchronizationContext sc = o as SynchronizationContext;             sc.Post(SetBackground11, null);         }








         void SetBackground11(Objeto o)
        {            this. BackColor = System.Drawing.SystemColors.ActiveCaptionText;         }

Supongo que te gusta

Origin blog.csdn.net/u014090257/article/details/127683090
Recomendado
Clasificación