Application is still running in memory after Appli

2020-02-26 10:04发布

The application I am building is still running in memory (checked in Task Manager) after it is closed using Application.Exit(). Because of this when I am running it again after closing it as mentioned above, I am getting this error "Only one instance at a time". Can you please tell me how to completely close my application?

3条回答
淡お忘
2楼-- · 2020-02-26 10:07

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.

查看更多
做自己的国王
3楼-- · 2020-02-26 10:12

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.

查看更多
迷人小祖宗
4楼-- · 2020-02-26 10:19

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.

查看更多
登录 后发表回答