代码之家  ›  专栏  ›  技术社区  ›  Malfist

退出时终止进程

  •  1
  • Malfist  · 技术社区  · 16 年前

    我的项目有一个创建进程的对象。如果使用此对象的Dispose函数,则会终止进程(或尝试终止进程)。但是,如果程序崩溃,它会让进程继续运行,并且不会清理。这会导致程序下次失败,因为它试图再次启动进程,但无法获得对它的锁定。

    如何确保此进程被终止?我总是使用在 using

    6 回复  |  直到 16 年前
        1
  •  3
  •   Marc Gravell    16 年前

    定义“崩溃”;有不同程度的崩溃。。。例如,如果某个东西主动终止了您的进程,那么您几乎没有机会运行任何Dispose/finalizers等—但是如果您的线程正常地展开(即使是通过异常),那么您应该是正常的(因为您正在使用 using

        2
  •  1
  •   JasonRShaver    16 年前

    我知道这会让我大喊大叫,但是环境。出口(). 这应该杀死一切,每一次=)它仍然是更好的,但做上述的选择。

    也:

    在你的程序.cs,在你打电话之前应用程序。运行(表格),请执行以下操作:

    AppDomain.CurrentDomain.UnhandledException未处理异常+=new UnhandledExceptionEventHandler(当前域\u UnhandledException);

    然后放入一个类似这样的处理程序:

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                String CurrentThreadName = System.Threading.Thread.CurrentThread.Name;
    
                if (e.IsTerminating)
                    Logger.Log("Program", 0, String.Format("Received fatal unhandled exception on '{0}' thread.  Exception will follow.", CurrentThreadName), MsgCategory.Critical, 50);
                else
                    Logger.Log("Program", 0, String.Format("Received unhandled exception on '{0}' thread.  Exception will follow.", CurrentThreadName), MsgCategory.Error, 25);
    
                if (e.ExceptionObject != null)
                    Logger.LogException("Program", String.Format("Unhandled System Exception on '{0}' thread.", CurrentThreadName), 50, (Exception)e.ExceptionObject);
            }
            catch
            {
            }
            if (e.IsTerminating)
            {
                Exception ThisException = (Exception)e.ExceptionObject;
                String CurrentThreadName = System.Threading.Thread.CurrentThread.Name;
                MessageBox.Show(
                        String.Format(Localization.GetString("fatal_error"), ThisException.GetType().ToString(), ThisException.Message, ThisException.StackTrace, CurrentThreadName)
                        , Localization.GetString("dialogtitle")
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Error
                        , MessageBoxDefaultButton.Button1);
            }
        }
    
        3
  •  1
  •   Pontus Gagge    16 年前

    如果要确保子进程终止,请考虑创建一个单独且更简单的看门狗进程,监视进程空间以终止其中一个进程。通常更容易防弹。无法保证清除代码在中的执行 情况。

    但是,对于如何创建子进程,您并没有提供足够的上下文来允许任何有关如何设计看门狗以了解父进程和子进程的相关建议。

        4
  •  0
  •   Daniel Earwicker    16 年前

    什么样的项目正在启动?即使第一个实例仍在运行,也应该可以启动第二个实例—启动进程不会锁定任何东西(除非程序本身做了一些奇怪的事情)。

    对象是如何启动进程的?如果它有一个句柄,它应该能够通过调用 TerminateProcess 应用程序编程接口。

    这里有一个非常棘手的解决方案来解决这个问题:call DebugActiveProcess ,传递子进程ID。

        5
  •  0
  •   i_am_jorf    16 年前

    如果家长停止回应,孩子就应该退出。如果孩子停止反应,杀死它,开始一个新的。

        6
  •  0
  •   abatishchev Karl Johan    16 年前
    Foo bar = new Foo();
    try
    {
      bar.DoSomething();
    }
    catch // if required
    {
    }
    finally
    {
      if(bar != null)
      {
        bar.Dispose(); // code from finally clause is ~always executed
      }
    }
    

    等于

    using(Foo bar = new Foo())
    {
      // ..
    }