C# begininvoke (control)

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void showMsg(string str)
        {
            if (!textBox1.InvokeRequired)
            {
                textBox1.Text = str;
            }
           
        }
        delegate void delegteSmg(string str);
        private void ShowThread(object lp)
        {
            string strText = "";
            int iCount=0;
            while(true)
            {
                textBox1.BeginInvoke(new delegteSmg(showMsg), 
                new string[] { string.Format("{0:D}--{1:D}", iCount++,Thread.CurrentThread.ManagedThreadId) });
                Thread.Sleep(10);
            }
        
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread showThread = new Thread(ShowThread);
            showThread.IsBackground = true;
            showThread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread showThread = new Thread(ShowThread);
            showThread.IsBackground = true;
            showThread.Start();
        }
    }
}

工作线程取更新主界面的例程。

猜你喜欢

转载自blog.csdn.net/wuan584974722/article/details/81588615