Application is still running in memory after Application.Exit() is called

☆樱花仙子☆ 提交于 2019-11-30 13:52:25

It seems that this is a Windows ap and you are calling System.Windows.Forms.Application.Exit() but there is a thread still running in the background. Have you tried

Application.ExitThread();

Environment.Exit();

You could kill the process as Jonesy mentioned, passing in the process ID of the process if it is a separate application than the current running process.

For that, you need to use the System.Diagnostics.Process namespace and loop through the currently running processes to get the right pid and then call kill on that pid.

GuacoIV

One time when I had odd behavior (crashing/freezing during Application.Exit()), I used Process.GetCurrentProcess().CloseMainWindow().

That function is in the System.Diagnostics namespace and seems to be better than Kill() since it does not force it to quit the same way.

Because of using Foreground Thread and Lybda Expession thread So, threads which will continue to run until the last foreground thread is terminated. In another way, the application is closed when all the foreground threads are stopped. that's why the application won't wait until the background threads are completed, but it will wait until all the foreground threads are terminated. So In that case we have to explicitly stop the all running threads by using

Environment.Exit(Environment.ExitCode);

This kept the memory managed perfectly, with no memory leak.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!