I’m using Microsoft Visual Studio 2005 on a PC running WinXP Pro. (Programming in C#, if that matters.) I’d like to know how to instantly terminate a Windows application when certain unacceptable conditions are detected.
Application.Exit() looks like it should work, but it doesn’t. Anyone know how to pull the plug from deep within a program?
void abort(void);
-which generates “This application has requested the Runtime to terminate it in an unusual way. Please contact the application’s support team for more information”
or just throw() and not have a catch()
In the c# doc, it looks like Process.Terminate() does what you want.
Process.Kill Method (System.Diagnostics) | Microsoft Learn System.Diagnostics.Process.Kill. Read the details because there are some idiosyncracies. The docs assume you’re killing a child process, not your own main process, so there might be some surprises there too.
Note that Process.Kill would be a bad thing to do in something like a business-layer class that might be called from anywhere, including a web server or via Remoting or from inside a WinService or from inside SQL Server.
But for code embedded directly in a WinForms or console app, this is the way to go. Note the examples in the CloseMainWindow that try to give it a chance to work first. They don’t fall back to .Kill, but you could certainly adapt them to do so. That’d be the cleanest.