windowform多线程学习(委托安全线程)

    技术2022-05-12  15

    using  System; using  System.Collections.Generic; using  System.ComponentModel; using  System.Data; using  System.Drawing; using  System.Linq; using  System.Text; using  System.Windows.Forms; using  System.Threading; namespace  weituoanquan {    public partial class Form1 : Form    {        public delegate void callhandler();        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Thread A = new Thread(SetText);//执行一个新的A线程.            A.Start();            //SetText();        }        public void Set()        {            this.textBox1.Text = "safe";        }        public void SetText()        {            if (this.textBox1.InvokeRequired)//如果有新的线程            {                callhandler call = new callhandler(Set);                textBox1.Invoke(call);//调用一个委托,执行一个被认为是线程安全的方法.            }            else            {                this.textBox1.Text = "NO";            }        }    }}

    最新回复(0)