跨线程访问

private void ShowIOStatus(bool isOpen)
        {
            if (IOStatusLB.InvokeRequired)
            {
                IOStatusLB.Invoke(new Action(() =>
                {
                    IOStatusLB.Text = isOpen ? "已连接" : "未连接";
                    IOStatusLB.BackColor = isOpen ? Color.Green : Color.Red;
                    if (isOpen)
                    {
                        ResetBT.Enabled = true;
                        StartBT.Enabled = true;
                    }
                    else
                    {
                        ResetBT.Enabled = false;
                        StartBT.Enabled = false;
                    }
                }));
            }
            else
            {
                IOStatusLB.Text = isOpen ? "已连接" : "未连接";
                IOStatusLB.ForeColor = isOpen ? Color.Green : Color.Red;
                if (isOpen)
                {
                    ResetBT.Enabled = true;
                    StartBT.Enabled = true;
                }
                else
                {
                    ResetBT.Enabled = false;
                    StartBT.Enabled = false;
                }
            }
        }
主要是这个语句
if (IOStatusLB.InvokeRequired)
            {
                IOStatusLB.Invoke(new Action(() =>
                {                       
                 //内容
                }));
            }

 

posted @ 2021-05-21 09:54  TONEY|Tang  阅读(58)  评论(0)    收藏  举报