I have a multi threaded C# application and it has reader writer lock,but it gives a timeout exception on some computers(not being able to acquire a lock in time) and I need to forcefully close all threads. how do I do it without getting any extra exceptions?
-
4`Environment.FailFast(int)` will kill your application as fast as possible. – vcsjones Nov 01 '11 at 18:47
-
Possible duplicate of http://stackoverflow.com/questions/1057151/application-exit – angularsen Mar 22 '15 at 11:36
5 Answers
I think the best solution to force application exit is to use the following line of code:
Environment.Exit(0)
Environment.FailFast()
ends up with a runtime exception.

- 460
- 5
- 17

- 3,531
- 1
- 26
- 46
If you want to kill program without any exception messages from system, after for example fatal error, you can use:
Process.GetCurrentProcess().Kill()

- 6,436
- 1
- 41
- 31
Environment.FailFast might be what you're looking for, but take care about the side-effects: no finalizers, finally blocks or anything else is run. It really does terminate the process NOW.

- 104,512
- 87
- 279
- 422
-
1ok,I used it and it gave me this exception : The runtime has encountered a fatal error. The address of the error was at 0x044f07bd, on thread 0xfb0. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. – armin Nov 01 '11 at 18:56
-
2It's worth mentioning the option of Environment.Exit. They are both guaranteed to exit pretty much immediately, but Exit does not generate a system event error event and runs the try/finally finalizer. – angularsen Mar 22 '15 at 11:34
-
1It isn't as "NOW" as you claim. I tried it and it takes a couple of seconds to finish its job. The other two top answers really DO work instantly. – Dan W Sep 26 '18 at 22:17
-
@DanW - Hmm... Perhaps because of all the error-reporting tasks it does? I'm reading the documentation now and I'm afraid that I can't find the subtle details of how the three approaches differ. Intuitively it seems to me that `Process.Kill()` should be the most brutal way of stopping the process. I suspect it calls the [`TerminateProcess()`](https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-terminateprocess) Win32 API function. – Vilx- Sep 27 '18 at 08:08
-
@DanW - `Environment.Exit()` sounds like a more graceful way, probably implemented in .NET itself; or maybe it's a wrapper around [`ExitProcess()`](https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-exitprocess). And finally, `Environment.FailFast()` seems more like a debugging aid - it does stop all the user code immediately, but .NET framework code still does some hosekeeping (writing event log entry, sending data to error reporting) before fully terminating the process. – Vilx- Sep 27 '18 at 08:11
To forcefully exit your application asap you can use Environment.FailFast()

- 158,293
- 28
- 286
- 335
This might be an irrelevant answer if you are looking for an pure C# code to terminate your application, but you can use a system native form of terminating processes such as taskkill
command, which will terminate your application for sure.
Give it a try. Add the system reference first.
using System.Diagnostics;
Wherever you need to terminate the process,
String process = Process.GetCurrentProcess().ProcessName;
Process.Start("cmd.exe", "/c taskkill /F /IM " + process + ".exe /T");
/F
- Forces to terminate./T
- Terminates the process tree (process and any other processes started by it)./IM
- Image Name or Process Name.

- 8,437
- 8
- 49
- 80