[英]Detect if control was disposed
在我的應用程序中,我有一個用戶控件,使用線程池執行異步操作。 線程池方法如下所示:
private void AsyncFunction(object state)
{
... do the calculation
//refresh the grid data on the UI thread
this.BeginInvoke(new MethodInvoker(() =>
{
... update the ui
}));
}
我的問題是,如果用戶關閉對話框...用戶控件被釋放,我得到異常:
在創建窗口句柄之前,無法在控件上調用Invoke或BeginInvoke。
你知道一種檢測對話框是否處理的方法嗎? 我不想在關閉時設置對話框設置的控件屬性。 還有另一種解決方法嗎?
謝謝,
拉杜
您可以使用Control.IsDisposed
屬性。
try
{
if(!this.IsDisposed)
{
this.BeginInvoke(new MethodInvoker(() =>
{
// update my control
}
));
}
}
catch ( InvalidOperationException )
{
// Do something meaningful if you need to.
}
您可以嘗試使用像EventWaitHandle這樣的同步對象來通知主線程即將終止的工作線程。 然后工作線程可以結束其執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.