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?
问题:
回答1:
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.
回答2:
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.
回答3:
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.