此代码以多种方式执行。当它由表单按钮执行时它工作(按钮启动一个线程,在循环中它调用这个方法=它工作)。但是当我从表单中的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.
为什么?
答案 0 :(得分:6)
你需要在if块的末尾返回 - 否则你将在正确的线程中调整它,然后在错误的线程中执行它。
换句话说(如果您剪切并粘贴代码而不是图片,这会更容易......)
private void resizeThreadSafe(int width, int height)
{
if (this.form.InvokeRequired)
{
this.form.Invoke(new DelegateSize(resizeThreadSafe,
new object[] { width, height });
return;
}
this.form.Size = new Size(width, height);
this.form.Location = new Point(0, SystemInformation.MonitorSize // whatever comes next
}
或者只是将方法的后半部分放在“else”块中。
答案 1 :(得分:1)
你需要写下这个:
if ( this.form.InvokeRequired ) {
this.form.Invoke( ...... );
return;
}
this.form.Size = new Sizte( ... );
OR
if ( this.form.InvokeRequired ) {
this.form.Invoke( ...... );
}
else {
this.form.Size = new Sizte( ... );
}
答案 2 :(得分:1)
根据您的编码风格,请在调用后立即使用返回,或将实际操作设为 else块。