Visual Studio question - terminating an application

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?

in C++, not sure about C#:

void exit( int status);

alternately:

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.

Ref this http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.exit(VS.71).aspx System.Windows.Forms.Application.Exit requests an orderly shutdown. If you’ve got unresponsive threads, you’ll hang rather than quit.

This is the next step up: Process.CloseMainWindow Method (System.Diagnostics) | Microsoft Learn System.Diagnostics.Process.CloseMainWindow is for orderly shutdowns. The verbiage says that to force a no-kidding “quit now no matter what” the correct technique is …

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.

Thanks - you guys are good.

I certainly should have thought of Throw without Catch. But Process.Kill() definitely gets the job done.