繁体   English   中英

当关闭记事本进程时,我正在获取异常,这是为什么?

[英]When closing a notepad process im getting exception why is that?

这是代码:

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

在Form1中,我有一个菜单,然后单击它,然后打开了一个新表单。在这里,我有一个按钮,单击可以使用notepad.exe打开日志文件(txt文件)。如果我先关闭记事本,然后关闭菜单窗体,则一切正常。 但是,如果我先关闭菜单窗体,然后尝试关闭记事本,则会出现异常:

异常错误是:在创建窗口句柄之前,无法在控件上调用Invoke或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: 

在这种情况下我应该如何保重? 如果用户先关闭菜单窗体,然后关闭记事本,它将抛出此异常。

由于如果在记事本窗口之前关闭菜单窗体时遇到错误,我建议您使用菜单窗体FormClosing EventHandler在菜单窗体关闭之前关闭记事本窗口。

从上面的链接:

FormClosing事件在窗体关闭时发生。 关闭表单后,将对其进行处置,从而释放与该表单关联的所有资源。 如果取消此事件,则表单保持打开状态。 若要取消窗体的关闭,请将传递给事件处理程序的FormClosingEventArgs的Cancel属性设置为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM