I have a process written in C# which has no forms or output whatsoever.
static void Main() {
MyBackgroundProcess process = new MyBackgroundProcess();
// begin background process
process.Start();
// keep application running in the background, run events
Application.Run();
}
I need to free up some resources and do other things when the application terminates (which should only be when the user running it logs off or if it's manually killed through the task manager; the process will never terminate itself).
What's the best way to handle this?
Things I've tried that haven't worked:
- Hooking an event to both Application.ApplicationExit and Application.ThreadExit
- Had MyBackgroundProcess implement IDisposable and wrapped the Main logic in a using()