Environment.Exit(int) 会用 treads 运行 非托管代码杀死我的应用程序吗?

Will Environment.Exit(int) kill my application with treads running unmanaged code?

我有一个 windows 服务,它在不同的线程中运行非托管代码(使用 DllImport)。

有时,非托管代码'hangs'。想想while (true) ;。发生这种情况时,我需要终止整个进程(它会自动启动另一个进程,因为它是一项 windows 服务)。

Environment.Exit(int)够吗?或者我需要例如Environment.FailFast(string)?

编辑:我无法 'test' 这个。冻结是随机发生的。

是的。 Environment.Exit 将杀死当前进程中的所有线程 运行,包括主线程(和进程本身)。

Environment.FailFast 将事件记录到应用程序日志中,然后终止进程和当前进程中的所有线程。

来自微软官方文档,Environment.Exit

Terminates this process and returns an exit code to the operating system.

更有用的是,文档继续说明:

  • 退出总是终止应用程序
  • Exit 立即终止应用程序,即使其他线程正在 运行

听起来 Environment.Exit 完全可以满足您的需求。