21-C#的委托简单使用-1

C#的委托简单使用

csharp 复制代码
namespace WindowsFormsApp1
{
    public delegate void setdetetest();//定义一个委托
    public partial class Form1 : Form
    {
        setdetetest detetest1;
        public Form1()
        {
            InitializeComponent();
            detetest1 = new setdetetest(run1);//委托实例化
        }

        private void run1()
        {
            textBox1.Text = "123456789";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th1 = new Thread(th1test);//开启一个线程。调用委托
            th1.Start();//开启线程
        }

        private void th1test()
        {
            this.Invoke(detetest1);//调用委托
        }
    }
}