当某些表单中存在长时间运行的代码(例如,数据加载期间)时,我在其他线程上显示等待表单(说“请稍候...”)。我给这样的形式:Control.Invoke错误:句柄尚未创建
m_PopProcessingThread = New Thread(New ThreadStart(
Sub()
m_PopProcessingForm = New WaitingForm(m_Message)
Application.Run(m_PopProcessingForm)
End Sub))
m_PopProcessingThread.Name = "Pop Processing Thread"
m_PopProcessingThread.SetApartmentState(ApartmentState.STA)
m_PopProcessingThread.Start()
然后,我隐藏它是这样的:
While m_PopProcessingForm Is Nothing OrElse Not m_PopProcessingForm.IsHandleCreated
Threading.Thread.Sleep(20) 'Wait a bit for the form to be created
End While
' Dispose of the pop processing form (by disposing of this form, thread is also exited)
m_PopProcessingForm.Invoke(Sub()
m_PopProcessingForm.Dispose()
End Sub)
此代码的伟大工程,但我刚接到一个顾客的bug报告:
Exception Type : System.InvalidOperationException
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
堆栈跟踪指向隐藏表单的代码部分。在Invoke
调用之前,如何创建句柄?我循环直到创建句柄?谢谢你的帮助。
使用一个BackgroundWork和/或任务它们正是为此目的而设计的。 – 2012-01-31 19:07:03
我知道这有点颠倒,通常应该是在另一个线程,而不是等待的形式。但考虑到目前的代码在99,999%的时间内工作,我宁愿修复它,而不是改变我的所有代码。 – 2012-01-31 19:13:03