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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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
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.
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.
One time when I had odd behavior (crashing/freezing during
Application.Exit()
), I usedProcess.GetCurrentProcess().CloseMainWindow()
.That function is in the
System.Diagnostics
namespace and seems to be better thanKill()
since it does not force it to quit the same way.