Friday, August 24, 2007

Single Instance of a .Net App - Correction

A bug that I found in the method described in my post Single Instance of a .Net App:
Apparently, in some cases, the local mutex variable is optimized out, making it ripe to be garbage collected. The result? The next instance does manage to create the mutex, rendering the entire method moot. This is inconsistent (for example, my demo app behaves like expected, other apps don't). The incosistency drove me mad for a couple of days, but I decided to stick with it until I get the result.

The solution? make the mutex variable static - that way the GC leaves it alone until the app exits.
I've corrected the code of the original post to reflect the fix.

To get to the root cause of this problem, I've used Visual Studio, Process Explorer (to see which mutexes are being held by which app), a software that shell remain nameless and a lot of help from my friend Jeff.

No comments: