我toolStripStatusLabel
在多线程中使用,虽然它是另一个与总是返回 false 的toolStripStatusLabel
我的 invokeRequired 方法交互的线程statusStrip
(我也强制创建句柄)。在这个线程中,我可以访问toolStripStatusLabel
(从 else 子句),更新它,但我的编辑无法显示在主 UI 中。这是我的代码:
public void safeThreaded()
{
Form2 form = new Form2();
StatusStrip ss = (StatusStrip)form.Controls["statusStrip1"];
ToolStripStatusLabel label = (ToolStripStatusLabel)ss.Items["toolStripStatusLabel1"];
string text = "Written by the background thread.";
if (!(ss.IsHandleCreated))
{
IntPtr handler = ss.Handle;
}
if (ss.InvokeRequired)
{
ss.Invoke(new updateTextCallback(updateText), new object[]{"Text generated on non-UI thread."});
}
else
{
// It's on the same thread, no need for Invoke
label.Text = text + " (No Invoke)";
MessageBox.Show(label.Text.ToString());
ss.Refresh();
}
}
private void updateText(string text)
{
Form2 form = new Form2();
StatusStrip ss = (StatusStrip)form.Controls["statusStrip1"];
ToolStripStatusLabel label = (ToolStripStatusLabel)ss.Items["toolStripStatusLabel1"];
label.Text = text;
}
public delegate void updateTextCallback(string text);