Code execution when application crashes

I have some cleanup code that I would like to execute at the end of execution, even if my C # application crashes (deletes temporary files, etc.). Is this possible to do in C #?

Thank!

+3
source share
6 answers

It depends on what you mean by "failure."

If you want to deal with any unhandled exceptions that might otherwise crash the application, you can attach an event handler to AppDomain.UnhandledException and then handle any errors in the event handler.

, .Net 4.0 AppDomain.FirstChanceException, , catch, .

, (, ), , , , , AppDomain.UnhandledException.

+8

, , , , , ..

. temp .. , temp, , , / .

, , , , . , , , .

, , , .

+5

, , .

, , .

[ . , AppDomain.UnhandledException, , . .]

+2

, , -, . , . , , - , . , , , .

+1

It depends on what you mean by crash. If you mean an unhandled exception, you can create a handler for Application.ThreadException. If an accident is caused outside of managed code, you can do nothing.

0
source

What is it for try {} finally {}? Wrap all your code in your mainmethod that should do this.

-3
source

Source: https://habr.com/ru/post/1737758/


All Articles