Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to immediately exit a Windows Forms .NET application?

In our application, we have a quite extensive exception handling mechanism. At some point in our error handling logic, we want to terminate the application -- right at this point with no further code execution.

Our current code use Environment.Exit() to do that. After a call to Environment.Exit(), some code is still executed. For instance, the GC may execute the finalizer of some objects (and that causes a problem in our case). We don't want that to happen. Is there a way to really kill our own process (a Win32 API call maybe)?

Of course, we don't want the end-user to see the Windows dialog that appears when a program crashes...

like image 335
Sylvain Avatar asked Sep 16 '25 03:09

Sylvain


2 Answers

Use the Environment.FailFast method:

Terminates a process but does not execute any active try-finally blocks or finalizers.

like image 141
Andrew Hare Avatar answered Sep 17 '25 19:09

Andrew Hare


Try Environment.FailFast

like image 39
Nick Avatar answered Sep 17 '25 19:09

Nick