Question

I have a C# application which makes use of the Microsoft Windows API Code Pack - in particular the Shell Extensions, which I use to monitor storage devices and media insertion and removal.

However when I attempt to close the app in Visual C# 2010 (Express) I then have to manually stop the debugger. It appears that there is a background loop in the Win API Code Pack that is still running, even when I manually dispose of the ShellObjectWatcher. The only way I can kill it is to manually stop the debugger.

The app is built in WPF.

Eventually, VisC#2010 gives up on trying to run the app under the debugger. You tell it to start debugging and it just doesn't. Only way to get it going again is to kill the app using Task Manager and then shutdown VC#2010 - go have a coffee - then start it up again. Odd. I suspect there is a hidden process or window hanging around which isn't being shut down when I try and clean up the app.

Any idea how I can clean up this ShellObjectWatcher a little more effectively?

Was it helpful?

Solution

To fix the bug in the Code Pack Shell project, add one line in MessageListener.WndProc()​:

case (uint)WindowMessage.Destr​oy:
 **_running = false;**
 break;

Now ThreadMethod() will exit the message loop.

OTHER TIPS

OK using System.Environment.Exit(0); fixes this issue. App shuts down and the debugger releases control. Brute force works in this case.

I bumped into the same issue, but solved it by setting the thread as a background thread (meaning, it is killed when the application stops):

in MessageListener.cs, in the constructor, at line 40, I have:

_windowThread = new Thread(ThreadMethod);
// The line below will force the thread to terminate when the app exits.
_windowThread.IsBackground = true;
_windowThread.SetApartmentState(ApartmentState.STA);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top