1

I'm using a C# library that has a pretty bad shutdown procedure and whenever I exit the application using Environment.exit, it throws an exception and my whole console application freezes with a popup message (debug/close/etc). I am running my application as a scheduled task so I'd prefer not to have to manually hit the close button every time it runs and crashes on exit. I've tried putting some try catches around it, and handling all exceptions, but it doesnt seem to work.

Is there a way to force an application exit and not allow any exceptions or anything that will cause it to crash?

Thanks -Jeff

  • 1
    What is the exception that you get when it exits? – ajw Aug 05 '14 at 11:43
  • 1
    Why are you using `Environment.Exit`? You should simply `return` from the `Main` function. `Environment.Exit` is a hack all in itself :) The exception is most likely thrown on a separate thread, which is why `try`...`catch` around the `Environment.Exit` call doesn't help - it's an unhandled exception on a different thread -> crash. – Luaan Aug 05 '14 at 11:44
  • Try putting break point and share the code where it breaks, this might solve your problem. – Salmaan Aug 05 '14 at 11:44
  • I believe `Envorinment.Exit` is the only way to return a console error code to the caller; or is it possible to `return` an `int` from the `Main` function? – Codor Aug 05 '14 at 11:45
  • 1
    @Codor Yeah, you just change the signature to return `int` instead of `void` and it will return that as the exit code. – Luaan Aug 05 '14 at 11:46
  • refer to [this post][1].
    Your solution seems to be there. [1]: http://stackoverflow.com/questions/3133199/net-global-exception-handler-in-console-application
    – Vladislav Aug 05 '14 at 11:46
  • you should not be forcing exist like so but if you must you shoul give it a return code as it's a console app. you should be calling `Application.Exit(0);` instead. But the exception your having you should add it's details in your post so people can help you further – Franck Aug 05 '14 at 11:47
  • @Luaan Thanks for the hint, I didn't know - the Visual Studio wizard for console applications defines `void` for the return of `Main`. – Codor Aug 05 '14 at 11:50
  • @Franck `Application.Exit(0);` is for winforms app, not console apps. – Sriram Sakthivel Aug 05 '14 at 11:53
  • @Codor, that's probably the default signature of `MAIN()` – Rahul Aug 05 '14 at 11:53
  • The main point is, killing the whole application from some library should never be seen as something normal and reasonable. It's invariably an exceptional state (and often a result of bad design). The application and the user should be in control, not some library method of yours. If you need to exit an application from a library method, you should signal the application that you're requesting an exit and let the application handle it, rather than doing `Process.Kill` on the process :) You just need a proper clean shutdown, as simple as that. – Luaan Aug 05 '14 at 11:57

0 Answers0