我已经尝试过了。
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
时没有错误
我的代码中有一个方法等待bool
来自另一个使用Thread在后台运行的方法。
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."));
关于c# - 跨线程操作无效: Control 'statusStrip' accessed from a thread other than the thread it was created on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008497/