使用 Process.Start 后,Environment.Exit() 导致我的应用程序崩溃

发布于 2024-09-26 18:58:37 字数 1107 浏览 4 评论 0原文

我有一个小表单,它创建两个后台工作线程,用于侦听来自两个单独的服务器进程的消息。当用户尝试关闭表单时,我处理 OnFormClosing 事件(或者他们可以单击“退出菜单项”),该事件在两个线程上调用 CancelAsync() 。然后,窗体将等待,直到两个线程的 IsBusy 属性都为“FALSE”,然后再调用 Environment.Exit(0)。

问题是:从这个表单中,用户可以启动一个单独的应用程序。这是在单击特定按钮时使用 Process.Start 完成的。如果用户通过表单创建了一个新进程,然后关闭了表单,它不会正常退出,而是会崩溃,并且我会收到这些窗口错误消息之一。 Application.Exit 不起作用,因为它不会由于某种我不知道的原因关闭表单。我确信两个线程都已完成执行,因为我处理了两个线程的 RunWorkerCompleted 事件。这是核心代码的外壳:

private void startProcess_buttonClick(sender, e)
{
      Process.Start(<process args>);
}


protected override OnFormClosing()
{
    e.Cancel = true;

    if (!thread1.IsBusy && !thread2.IsBusy)
       Environment.Exit(0);

    stopThreads();
}
private void stopThreads()
{
   if (thread1.IsBusy)
       thread1.CancelAsync();

   if (thread2.IsBusy)
       thread2.CancelAsync();
}

private void thread1_RunWorkerCompleted(sender, e)
{
       if (!thread2.IsBusy)
          Environment.Exit(0);
}


private void thread2_RunWorkerCompleted(sender, e)
{
       if (!thread1.IsBusy)
          Environment.Exit(0);
}

对于导致 Environment.Exit 崩溃的原因有什么想法吗?

I have a small form that creates two background worker threads which listen for messages from two separate server processes. When the user attempts to close the form, I handle the OnFormClosing event (or they can click an 'Exit menu item) which calls CancelAsync() on both threads. The form then waits until the IsBusy property for both threads is "FALSE" before calling Environment.Exit(0).

Here is the catch: From this Form, the user is able to launch a separate application. This is done using Process.Start when a specific button is clicked. If the user has created a new process via the Form, and then closes the Form, instead of exiting gracefully it crashes and I get one of those windows error messages. Application.Exit doesn't work because it won't close the form for some reason unbeknown to me. I'm certain that both threads are finished executing because I handle the RunWorkerCompleted event for both threads. Here is a shell of the core code:

private void startProcess_buttonClick(sender, e)
{
      Process.Start(<process args>);
}


protected override OnFormClosing()
{
    e.Cancel = true;

    if (!thread1.IsBusy && !thread2.IsBusy)
       Environment.Exit(0);

    stopThreads();
}
private void stopThreads()
{
   if (thread1.IsBusy)
       thread1.CancelAsync();

   if (thread2.IsBusy)
       thread2.CancelAsync();
}

private void thread1_RunWorkerCompleted(sender, e)
{
       if (!thread2.IsBusy)
          Environment.Exit(0);
}


private void thread2_RunWorkerCompleted(sender, e)
{
       if (!thread1.IsBusy)
          Environment.Exit(0);
}

Any ideas as to what would be causing the crash on Environment.Exit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-10-03 18:58:37

请尝试一下

Application.Exit()

既然您正在运行 Windows 窗体, 。我自己没有做过测试用例,但我很确定 Winforms API 对于通过使用内核 API 的环境立即终止进程感到不满意

我还发现了这个: http://geekswithblogs.net/mtreadwell/archive/ 2004/06/06/6123.aspx

Try

Application.Exit()

since you're running Windows Forms. I haven't made a test case by myself, but I'm pretty sure that Winforms APIs are unhappy about the immediate termination of a process via Environment, which uses kernel APIs.

I also found this: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

坐在坟头思考人生 2024-10-03 18:58:37

怎么样

protected override OnFormClosing()
{
    e.Cancel = true;

    if (!thread1.IsBusy && !thread2.IsBusy)
       this.Close();

    stopThreads();
}
private void thread1_RunWorkerCompleted(sender, e)
{
       if (!thread2.IsBusy)
          this.Close();
}
private void thread2_RunWorkerCompleted(sender, e)
{
       if (!thread1.IsBusy)
          this.Close();
}

How about

protected override OnFormClosing()
{
    e.Cancel = true;

    if (!thread1.IsBusy && !thread2.IsBusy)
       this.Close();

    stopThreads();
}
private void thread1_RunWorkerCompleted(sender, e)
{
       if (!thread2.IsBusy)
          this.Close();
}
private void thread2_RunWorkerCompleted(sender, e)
{
       if (!thread1.IsBusy)
          this.Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文