Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exiting a C# winforms application

I have an application that imports data from Excel. However, when I run the winforms app and I intrupt the application, using System.Windows.Forms.Application.Exit(); I can still see the "MyAppName".vshost32.exe running in task manager.

When I exit the application in debug mode, the form closes, but the VS IDE is not "stopped".

How do I ensure the application ends correctly.

like image 288
Troy Avatar asked Dec 14 '11 16:12

Troy


People also ask

What is exit () function in C?

The exit() function in C. The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program.

How do you exit AC program?

exit() Terminate the program: The exit() function is used to terminate program execution and to return to the operating system. The return code "0" exits a program without any error message, but other codes indicate that the system can handle the error messages.

What is exit function?

The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. The general form of the exit() function is as follows − void exit (int code);


1 Answers

Your call to Application.Exit() is working fine. The MyAppName.vshost32.exe executable is a host for debugging purposes. It runs whilst you have a project open in Visual Studio, regardless of if there is an active debugging session.

Update: Ok, I misunderstood. The above is true, but you're probably having problems with hung threads in the background. You need to terminate your threads to make it close properly. Asher's answer covers this. If you're just trying to do a super-hacky quick-and-dirty kill, you can use the following (though I take no responsibility for side effects, since it's extremely hacky):

System.Diagnostics.Process.GetCurrentProcess().Kill();

like image 76
Polynomial Avatar answered Oct 08 '22 14:10

Polynomial