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:
我該如何照顧這種情況?如果用戶首先關閉菜單表單然後記事本,它會拋出此異常。
如何關閉Windows窗體FormClosing EventHandler中的NotePad窗口 –
從表單關閉時訂閱的委託/事件中移除'processNotePad_Exited'。 – Igor
你的意思是,如果用戶試圖先關閉菜單表單,那麼它首先會關閉記事本?因此,在FormClosing事件的菜單中執行它將首先關閉記事本。 – user1196715