我试图从 STA 线程内部访问 GUI 元素(ToolStripStatusLabel)。我在尝试调用它时遇到了各种各样的问题,那是因为现在我知道ToolStripStatusLabel没有调用,我的最新错误是: 错误 1 'System.Windows.Forms.ToolStripStatusLabel' 不包含“调用”的定义
我似乎无法在没有调用的情况下更新它,因为我收到错误: 发生 System.NullReferenceException'
我在这里有什么选择?我将如何执行此操作,从 STA 线程内部更改 GUI 线程上的 statuslabel.text。
我已经使用了这里找到的所有标准调用代码和控制扩展,但仍然存在相同的问题。
要求的代码:
private void buttonclick(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
var thread = new Thread(() =>
{
var ie = watinBrowser();
log("[!] Starting Application");
log("blahblah");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void log(string logIt)
{
statusUpdate(logIt);
}
private void statusUpdate(string text)
{
if (this.statusStrip1.InvokeRequired)
{
this.statusStrip1.Invoke(new MethodInvoker(() => this.toolStripStatusLabel1.Text = text));
}
else
{
this.toolStripStatusLabel1.Text = text;
}
}