C#跨类跨线程通过静态方法操作控件

 主窗体写法:

 private delegate void CrossThreadOperationControl();
        /// <summary>
        /// 创建一个后台进程 进行跨线程操作控件
        /// </summary>
        public  void BackgroundProcess()
        {
            // 将代理实例化为一个匿名代理 
            CrossThreadOperationControl CrossDelete = delegate ()
            {
                //调用filehandle类中的recivewebsocket方法 将label控件作为参数传进去
                FileHandle.ReciveWebsocket(PromatMess);
            };
            PromatMess.Invoke(CrossDelete);
            
        }
类中函数写法:
 public static void ReciveWebsocket(Label label)
        {
 
            try
            {
                  label.Text = "服务器已连接";
            }
            catch (Exception)
            {       
                label.Text = "服务器已断开";                
            }
            
        }
然后创建新线程
Thread thread = new Thread(BackgroundProcess);
 thread.IsBackground = true;
 thread.Start();

猜你喜欢

转载自www.cnblogs.com/Hero-/p/9263748.html