Search code examples
c#.netterminate

Termination scenario in C#


I want to execute some code (clear up resoucrse) on termination my WinFormsApplication using Windows TaskManager. How can I do it? I am terminating my application using Process Tab of TaskManager.


Solution

  • If your application is forcibly closed I'm not sure there is a reliable way to ensure you clean up resources. I'm assuming your resources are non-standard ones that you, and not Windows, manage.

    There are two possibilities. The first is to install a windows service application that watches your app and ensures its resources are cleaned up when your application is killed. This requires some inter-process communication and a fair amount of coding.

    The second is to ensure resources are clean on application startup. Check to see if your resources are out there hanging around when you run, and clean them up before continuing execution.