繁体   English   中英

跨线程操作无效:从不是在其上创建线程的线程访问控件“ statusStrip”

[英]Cross-thread operation not valid: Control 'statusStrip' accessed from a thread other than the thread it was created on

我已经尝试过了。 https://stackoverflow.com/a/142069/2126472

toolStripStatusLabel1.InvokeRequired

但是toolStripStatusLabel1没有InvokeRequired

我也尝试过 https://stackoverflow.com/a/15831292/2126472

它像无效参数上的错误

SetTextCallback d = new SetTextCallback(SetText); form.Invoke(d, new object[] { form, ctrl, text });

当我使用ThreadHelperClass.SetText(this, toolStripStatusLabel1, "This text was set safely.");

但当我使用textBox时没有错误

我的代码具有等待使用Thread在后台运行的另一种方法的bool方法。

Thread t = new Thread(TestRemoteDB);
t.Start();
// In TestRemoteDB() will call updateRemoteDBStatus()

像这样

private void updateRemoteDBStatus(bool successful)
        {
            if (successful)
            {
                toolStripStatusLabel4.Text = "OK!";
                toolStripStatusLabel4.ForeColor = Color.Green;
            }
            else
            {
                toolStripStatusLabel4.Text = "Error!";
                toolStripStatusLabel4.ForeColor = Color.Red;
            }
        }

尝试这个:

this.BeginInvoke((Action)(() => toolStripStatusLabel1.Text = "This text was set safely."));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM