c# - 跨线程操作无效: Control 'statusStrip' accessed from a thread other than the thread it was created on

标签 c# multithreading winforms

我已经尝试过了。
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/

相关文章:

C#:为什么 0xFFFFFFFF 在表示 -1 时是一个 uint?

c# - 使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒

c# - 输入第四位数字时引发文本框事件

.net - 为什么我的 DataGridView 拒绝显示底部数据以及如何修复它?

javascript - Windows 窗体 Web 浏览器控件不将 "do"解释为标识符

c# - 从基本系统类型到字符串的转换是否可逆?

c# - Visual C#中的奇怪编译错误

ruby - Ruby 中的多线程

multithreading - 线程编程中的锁和原子操作之间的关系

java - 多线程socket通信场景