【发布时间】:2008-12-03 13:56:12
【问题描述】:
此代码以多种方式执行。当它由表单按钮执行时,它起作用(按钮启动一个线程并在循环中调用此方法=它起作用)。但是当我从表单中的 BackgroundWorker 调用该方法时,它不起作用。
使用以下代码:
private void resizeThreadSafe(int width, int height)
{
if (this.form.InvokeRequired)
{
this.form.Invoke(new DelegateSize(resizeThreadSafe),
new object[] { width, height });
}
this.form.Size = new Size(width, height); // problem occurs on this line
this.form.Location = new Point(0, 0); // dummy coordinate
}
然后在包含this.form.Size = ... 的行上出现以下异常:
InvalidOperationException was unhandled
Cross-thread operation not valid: Control 'Form1' accessed from a thread other
than the thread it was created on.
为什么?
【问题讨论】:
标签: c# .net winforms multithreading