該代碼通過多種方式執行。當它由表單按鈕執行時,它就起作用(按鈕啓動一個線程,並在循環中調用此方法=它工作)。但是當我在窗體中從我的BackgroundWorker調用該方法時,它不起作用。幫我解決這個CrossThread問題?
用下面的代碼:
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.
爲什麼?