2013-01-04 56 views
1

這是代碼:當關閉一個記事本進程即時通知異常爲什麼?

private void processNotePad_Exited(object sender, EventArgs e) 
     { 
      this.Invoke((MethodInvoker)delegate 
      { 
       View_Log_File.Enabled = true; 
      }); 
     } 

在Form1我有一個菜單我點擊它,一個新的形式打開后里面有我一直在使用的notepad.exe 點擊一個按鈕至極打開日誌文件(txt文件)如果我先關閉記事本然後關閉菜單表單就可以了。 但是,如果我先關閉菜單的形式和後試圖關閉記事本即時得到一個例外:

異常錯誤是:調用或BeginInvoke可直到窗口句柄已創建不能在一個控件調用

System.InvalidOperationException was unhandled 
    HResult=-2146233079 
    Message=Invoke or BeginInvoke cannot be called on a control until the window handle has been created. 
    Source=System.Windows.Forms 
    StackTrace: 
     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) 
     at mws.SettingsMenu.processNotePad_Exited(Object sender, EventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\SettingsMenu.cs:line 148 
     at System.Diagnostics.Process.OnExited() 
     at System.Diagnostics.Process.RaiseOnExited() 
     at System.Diagnostics.Process.CompletionCallback(Object context, Boolean wasSignaled) 
     at System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context(Object state, Boolean timedOut) 
     at System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context_f(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object state, Boolean timedOut) 
    InnerException: 

我該如何照顧這種情況?如果用戶首先關閉菜單表單然後記事本,它會拋出此異常。

+0

如何關閉Windows窗體FormClosing EventHandler中的NotePad窗口 –

+0

從表單關閉時訂閱的委託/事件中移除'processNotePad_Exited'。 – Igor

+0

你的意思是,如果用戶試圖先關閉菜單表單,那麼它首先會關閉記事本?因此,在FormClosing事件的菜單中執行它將首先關閉記事本。 – user1196715

回答

1

由於如果菜單窗體在記事本窗口之前關閉時出現錯誤,我建議您在菜單窗體關閉之前使用菜單窗體FormClosing EventHandler關閉記事本窗口。

從以上鍊接:

作爲表單被關閉時發生的FormClosing事件。當表單關閉時,它將被釋放,釋放與表單相關的所有資源。如果您取消此活動,表單將保持打開狀態。要取消表單的關閉,請將傳遞給您的事件處理程序的FormClosingEventArgs的Cancel屬性設置爲true。

相關問題