heltonbiker heltonbiker -4 years ago 121
C# Question

Giving a WPF Application the opportunity to cleanup its resources even when terminated externally

Currently I am working with a WPF Application that is started by another process, and can be terminated by that other process at any time.

It currently drives a data-acquisition usb device that is being left in an unresponsive state due to unproper device disconnection.

I want to know what would be the appropriate way for it to have the opportunity to release hardware resources before being terminated.

  1. Is there an event that is always run by the Application instance?

  2. Would it be possible for me to create a thread that outlives the Application instance and sends the

Answer Source

It sounds like your primary problem is that the USB device is buggy. Anything can happen to a Windows process at any time; a device needs to be able to deal with this. If the process with which it was interacting disappears, the device needs to be able to recognize this and deal with it. I don't know the specifics (not being a driver author myself), but there are plenty of USB devices out there that don't just stop working just because the program that was using it was forcefully terminated.

That said, that brings me to the second issue: from your description, it seems that the "other process" is forcefully terminating your program. Why? There are more graceful ways to have a program exit. At the very least, just send WM_CLOSE the the main window, as if a user was clicking the "Close" button, pressing Alt+F4, etc. Even better, provide some type of inter-process communication (e.g. named pipe) to allow the parent process to give specific commands, including one to close, to your program.

If your process is being forcefully terminated, there's nothing you can do about it, not from within that process anyway. You don't get to run any shutdown code, nor can you create special threads that keep going even after the process is killed.

One option would be to write yet another program, a "watchdog" that monitors the status of your program. If your program is terminated, then the watchdog could clean up the USB device. But that's not a panacea; someone could kill the watchdog process also, and leave you right back where you started.

But if you can arrange things so that there's even the least amount of cooperation between the other process and yours, then you can take advantage of the usual mechanisms, such as the Application.Exit event or a window's Closing or Closed event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download