Environment.Exit () crashes my application after using Process.Start

I have a small form that creates two workflow threads that listen on messages from two separate server processes. When the user tries to close the form, I handle the OnFormClosing event (or they can click the Exit element), which calls CancelAsync () for both threads. The form then waits until the IsBusy property for both threads is "FALSE" before invoking the environment. Exit (0).

Here's the catch: from this form, the user can run a separate application. This is done using Process.Start when a particular button is clicked. If the user created a new process through the form, and then closes the form, and does not gracefully fail, I get one of these Windows error messages. Application.Exit does not work because it does not close the form for some reason without the knowledge. I am sure that both threads are completed because I am handling the RunWorkerCompleted event for both threads. Here is the main code shell:

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 on what might cause the environment to crash. Exit?

+3
source share
2 answers

Try

Application.Exit()

Windows Forms. , , API Winforms , API .

: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

+3

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();
}
0

Source: https://habr.com/ru/post/1768798/


All Articles