C#委托函数的使用

1、不传参数的使用:

        // 委托函数
        public delegate void DelegateShowInfo();
        DelegateShowInfo delegateShowInfo;
         public void showInfo()
        {
            // 跨线程安全调用
            if (this.InvokeRequired)
            {
                while (!this.IsHandleCreated)
                {
                    //解决窗体关闭时出现“访问已释放句柄“的异常
                    if (this.Disposing || this.IsDisposed)
                    {
                        logger.Error("访问已释放句柄!");
                        return;
                    }
                }
                delegateShowInfo = new DelegateShowInfo(showInfo);
                this.Invoke(delegateShowInfo);
            }
            else
            {
                   //界面显示数据。。。
            }
      }

2、传递shit实体类的使用:

        // 委托函数
        public delegate void DelegateShowInfo(IDCardInfo cardinfo);
        DelegateShowInfo delegateShowInfo;

        public void showInfo(Bean bean)
        {
            // 跨线程安全调用
            if (this.InvokeRequired)
            {
                while (!this.IsHandleCreated)
                {
                    //解决窗体关闭时出现“访问已释放句柄“的异常
                    if (this.Disposing || this.IsDisposed)
                    {
                        logger.Error("访问已释放句柄!");
                        return;
                    }
                }
                delegateShowInfo = new DelegateShowInfo(showInfo);
                this.Invoke(delegateShowInfo, new object[] { bean});
            }
            else
            {
                //显示界面信息操作。。
             }
         }

猜你喜欢

转载自blog.csdn.net/u012716909/article/details/81584065