在混合托管/非托管c ++ / CLI进程关闭期间关闭线程

时间:2022-09-01 19:03:10

I'm working on a mixed managed/native application using c++/CLI.

我正在使用c ++ / CLI处理混合托管/本机应用程序。

I know that the CLR will suspend all managed threads on (a clean) shutdown, but what about the unmanaged ones? Is it possible for the unmanaged threads to still be running, while the CLR runtime is shutting down/freeing memory/running finalizers?

我知道CLR将暂停所有托管线程(干净)关闭,但是那些非托管线程呢?是否有可能非托管线程仍在运行,而CLR运行时正在关闭/释放内存/正在运行的终结器?

2 个解决方案

#1


I found further information on this issue:

我发现了有关此问题的更多信息:

Managed and Unmanaged Threading in Microsoft Windows

Microsoft Windows中的托管和非托管线程

Process shutdown in c++/CLI mixed executable

c ++ / CLI混合可执行文件中的进程关闭

#2


Note that the CLR will only suspend managed threads with property IsBackGround set to true. Any foreground threads that are still running will leave the application 'hanging' waiting for all foreground threads to finish.

请注意,CLR将仅挂起属性IsBackGround设置为true的托管线程。任何仍在运行的前台线程都将使应用程序“挂起”等待所有前台线程完成。

As far as unmanaged threads are concerned, the CLR has no way of knowing about them, so yes, they will continue running unless you write code to terminate them.

就非托管线程而言,CLR无法了解它们,所以是的,它们将继续运行,除非您编写代码来终止它们。

May I ask why your application cannot shut down the threads gracefully at the point where it decides to terminate rather than leaving it up to the environment to do your cleanup?

请问为什么你的应用程序无法在它决定终止时优雅地关闭线程而不是将它留给环境来进行清理?

#1


I found further information on this issue:

我发现了有关此问题的更多信息:

Managed and Unmanaged Threading in Microsoft Windows

Microsoft Windows中的托管和非托管线程

Process shutdown in c++/CLI mixed executable

c ++ / CLI混合可执行文件中的进程关闭

#2


Note that the CLR will only suspend managed threads with property IsBackGround set to true. Any foreground threads that are still running will leave the application 'hanging' waiting for all foreground threads to finish.

请注意,CLR将仅挂起属性IsBackGround设置为true的托管线程。任何仍在运行的前台线程都将使应用程序“挂起”等待所有前台线程完成。

As far as unmanaged threads are concerned, the CLR has no way of knowing about them, so yes, they will continue running unless you write code to terminate them.

就非托管线程而言,CLR无法了解它们,所以是的,它们将继续运行,除非您编写代码来终止它们。

May I ask why your application cannot shut down the threads gracefully at the point where it decides to terminate rather than leaving it up to the environment to do your cleanup?

请问为什么你的应用程序无法在它决定终止时优雅地关闭线程而不是将它留给环境来进行清理?