views:

257

answers:

1

I am using a .NET ActiveX control to an interface with a USB device. The supplier of the control says the we must call the control's Shutdown() method when we are through using the device. This is problematic for a me as it is not easy to determine when the users are through using the control because it is used between pages and I can only tell when a page is unloaded, not when the user leaves the application.

Doesn't the browser notify all ActiveX controls when it closes and can't all the resources used by the control be taken care of then?

+1  A: 

The short answer is "no, you have no way of knowing when the browser is going away."

The long answer is as follows:

  1. In IE6 and IE7 you can have BHOs in each tab, and maintain a global, process-wide count of open tabs by atomically incrementing a count when each BHO gets constructed, and atomically decrementing the count when you get a DWebBrowserEvents2::OnQuit message. Then, when the count gets to one, and you get an OnQuit message, you know you're the last tab and the process is going away.
  2. In IE8 you're out of luck because it does the process separation thing, and even then you could try to rig something up, but tab host processes don't go away when all their tabs are closed. They stick around for awhile in case a new tab opens.

But none of this really matters, since you just want to cleanup when your control is destroyed, which is when the page navigates. I'm not sure what you mean by "[the control] is used between pages", since that really isn't supported.

The proper way to know your control is going away is to implement IOleObject and IE will call IOleObject::SetClientSite(NULL) when it is time for you to go away. When you get the NULL site, you cleanup.

jeffamaphone
The control has state and is used on multiple pages so I cannot call Shutdown() in the onunload event the because the next page might also be using the control.
BeWarned
So, one page creates an ActiveX control, and then it just sticks around forever? That doesn't seem good. It's site will become invalid, etc.
jeffamaphone
The control gets instantiated each time we navigate to another page, its talking to a scanner which is stateful. I would like the .Net control shutdown the scanner when the browser closes, instead when we navigate between pages.
BeWarned