# Visual Studio question - terminating an application

**URL:** <https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472>\
**Category:** Factual Questions\
**Created:** [January 22, 2008, 5:40am UTC](https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472 "2008-01-22T05:40:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [January 22, 2008, 5:40am UTC](https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472/1 "2008-01-22T05:40:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![squeegee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/squeegee/32/14537_2.png) [@squeegee](https://boards.straightdope.com/u/squeegee)\
**Post date:** [January 22, 2008, 6:49am UTC](https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472/2 "2008-01-22T06:49:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [January 22, 2008, 1:03pm UTC](https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472/3 "2008-01-22T13:03:18Z")

</div>

Ref this [http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.exit(VS.71).aspx](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](http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow(VS.71).aspx) 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](http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx) 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.

---

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [January 22, 2008, 4:54pm UTC](https://boards.straightdope.com/t/visual-studio-question-terminating-an-application/434472/4 "2008-01-22T16:54:42Z")

</div>

Thanks - you guys are good.

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