C#子线程调用主线程控件

代码如下:

private void ShowMsg(string msg)
        { 
            if (this.InvokeRequired) 
            { 
                this.Invoke(new Action<string>(ShowMsg), msg); 
            } 
            else
            {
                this.txbLog.AppendText(msg);
            } 
        }

private void btnLive_Click(object sender, EventArgs e)
        {
            if (this.btnLive.Text == "Live")
            {
                islive = true;
                this.btnLive.Text = "Manual";
                thread1 = new Thread(Live);
                thread1.Start();
            }
            else
            {
                islive = false;
                this.btnLive.Text = "Live";
                thread1.Abort();
            }
        }

        private void Live()
        {
            while (islive)
            {
                string result="123";
                this.ShowMsg(result);
                Thread.Sleep(500);
            }
        }

猜你喜欢

转载自www.cnblogs.com/kaqiu/p/9155692.html