C#子线程访问主线程UI

 private delegate void SetShootRecoordTextCallback(string text);
        //在给textBox1.text赋值的地方调用以下方法即可
        private void SetShootRecoordText(string text)
        {
            // InvokeRequired需要比较调用线程ID和创建线程ID
            // 如果它们不相同则返回true
            if (this.textBox_ShootRecoord.InvokeRequired)
            {
                SetShootRecoordTextCallback d = new SetShootRecoordTextCallback(SetShootRecoordText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox_ShootRecoord.Text = text;
                textBox_ShootRecoord.SelectionStart = textBox_ShootRecoord.Text.Length;
                textBox_ShootRecoord.ScrollToCaret();
            }            
        }         

猜你喜欢

转载自blog.csdn.net/zouxin_88/article/details/80365066