C#委托的使用

在线程中,常常需要使用委托去改变控件的状态,委托实际就是一个函数指针。

C#使用委托的一般结构如下:

        //Fun函数代码
        private void fun(string str)
        {
            Show(str);
        }
        
        //声明委托:
        public delegate void MyDelegate(string str);


        //声明方法:
        private void Show(string str)
        {
            if (TextBoxReceive.InvokeRequired)
            {
                MyDelegate _myDelegate = new MyDelegate(Show);
                this.Invoke(_myDelegate, new object[] { str });
            }
            else
            {
                TextBoxReceive.Text = str;
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_31094099/article/details/80461185