Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 交叉线程操作无效:控件';statusStrip';从创建它的线程以外的线程访问_C#_Multithreading_Winforms - Fatal编程技术网

C# 交叉线程操作无效:控件';statusStrip';从创建它的线程以外的线程访问

C# 交叉线程操作无效:控件';statusStrip';从创建它的线程以外的线程访问,c#,multithreading,winforms,C#,Multithreading,Winforms,我试过这个。 如toolStripStatusLabel1.InvokeRequired 但是toolStripStatusLabel1没有invokererequired 我也试过这个。 它会出现错误,就像上面的无效参数一样 SetTextCallback d=新的SetTextCallback(SetText); 调用(d,新对象[]{form,ctrl,text}) 当我使用 ThreadHelperClass.SetText(这是toolStripStatusLabel1,“此文本已

我试过这个。

toolStripStatusLabel1.InvokeRequired

但是
toolStripStatusLabel1
没有
invokererequired

我也试过这个。

它会出现错误,就像上面的无效参数一样

SetTextCallback d=新的SetTextCallback(SetText);
调用(d,新对象[]{form,ctrl,text})

当我使用
ThreadHelperClass.SetText(这是toolStripStatusLabel1,“此文本已安全设置”)

但当我使用
textBox

我的代码有一个方法,它等待另一个使用线程在后台运行的方法的
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."));
试试这个:

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

您可以粘贴完整的代码吗?您可以粘贴完整的代码吗?的可能副本因为ToolStripStatusLabel没有BeginInvoke方法,所以无法工作。即使这样做了,也没有意义,因为ThreadHelperClass已经在使用Invoke在UI线程上执行代码。它不会编译,因为ToolStripStatusLabel不是控件,因此不能与OP链接的ThreadHelperClass一起使用。Lapsus,我的意思是“this”,并写了“ToolStripStatusLabel”。我认为这是一项工作,为我节省了大量代码。非常感谢@Marko Juvančič。这行不通,因为ToolStripStatusLabel没有BeginInvoke方法。即使这样做了,也没有意义,因为ThreadHelperClass已经在使用Invoke在UI线程上执行代码。它不会编译,因为ToolStripStatusLabel不是控件,因此不能与OP链接的ThreadHelperClass一起使用。Lapsus,我的意思是“this”,并写了“ToolStripStatusLabel”。我认为这是一项工作,为我节省了大量代码。非常感谢@Marko Juvančič。