Application still running in memory after Application.Exit ()

The application that I create still works in memory (marked in the task manager) after closing it with Application.Exit() . Because of this, when I start it again after closing, as mentioned above, I get this error "Only one instance at a time." Could you tell me how to completely close my application?

+6
source share
3 answers

This seems to be a Windows ap, and you call System.Windows.Forms.Application.Exit (), but there is a thread still running in the background. You tried

 Application.ExitThread(); Environment.Exit(); 

You can kill a process, as Jonesy mentioned, passing the process ID if it is a separate application than the current current process.

To do this, you need to use the System.Diagnostics.Process namespace and scroll through the currently running processes to get the correct pid, and then call kill on that pid.

+7
source

Once, when I had strange behavior (crashing / freezing during Application.Exit() ), I used Process.GetCurrentProcess().CloseMainWindow() .

This function is located in the System.Diagnostics namespace and is apparently better than Kill() because it does not force it to leave the same path.

+2
source

Due to the use of Foreground Thread and Lybda Expession thread So, the threads that will continue to work until the last thread of the foreground is completed. In another way, the application closes when all the threads of the foreground stop. therefore, the application will not wait for the completion of background threads, but will wait for the completion of all threads of the foreground. So, in this case, we must explicitly stop all running threads using

Environment.Exit(Environment.ExitCode);

This ensured that the memory worked correctly without memory leak.

0
source

Source: https://habr.com/ru/post/973140/


All Articles