【发布时间】:2013-07-06 06:09:55
【问题描述】:
我收到一个错误"Cross-thread operation not valid: Control 'AllOtherStatus' accessed from a thread other than the thread it was created on."
我有这个代码:_output设置为AllOtherStatus,看调试器,_output.InvokeRequired是false
在我更改了一个不使用这段代码的不相关类之前,这段代码运行良好。代码到达 else 语句然后抛出异常。
private void Thread(Object p)
{
lock (this)
{
if (_output.InvokeRequired)
{
if(s!= null)
_output.Invoke(new MethodInvoker(delegate { _output.AppendText(s); }));
}
else
_output.AppendText(s);
s = null;
}
}
所以我的问题是,为什么 _output.InvokeRequired 在应该返回 true 时却突然返回 false?
【问题讨论】:
-
AllOtherStatus是什么类型的控件,它是在哪里创建的? -
它是一个
TextBox,它是在InitializeComponent();MainForm下的第一行代码中用windows窗体创建的 -
不,至少我不这么认为。它是在 Windows 窗体中使用设计器创建的。
-
注释掉
lock(this)还会报错吗? -
是的,我仍然遇到同样的错误
标签: c# multithreading