publicvoid
asked on
Mutex different on debug than on release?
I have made a program that is only supposed to allow 1 copy to run at a time. Here is the Main()
[STAThread]
static void Main()
{
bool mutexWasCreated;
Mutex m = new Mutex(true, "PgrRn", out mutexWasCreated);
if(!mutexWasCreated)
{
//
// Another instance of the application already exists!
//
bool ret;
IntPtr hWnd = FindWindow(null, "Pgl");
if(hWnd != IntPtr.Zero)
{
ShowWindow(hWnd,9);
ret = SetForegroundWindow(hWnd);
}
}
else
{
Application.EnableVisualSt yles();
Application.Run(new Form1());
}
}
Everything works great in debug mode, but in Release, it allows more than one copy to start. Hmm...
I'm in a bit of a hurry, so i'll put the points up to 500.
Thank you,
John Gjonola
[STAThread]
static void Main()
{
bool mutexWasCreated;
Mutex m = new Mutex(true, "PgrRn", out mutexWasCreated);
if(!mutexWasCreated)
{
//
// Another instance of the application already exists!
//
bool ret;
IntPtr hWnd = FindWindow(null, "Pgl");
if(hWnd != IntPtr.Zero)
{
ShowWindow(hWnd,9);
ret = SetForegroundWindow(hWnd);
}
}
else
{
Application.EnableVisualSt
Application.Run(new Form1());
}
}
Everything works great in debug mode, but in Release, it allows more than one copy to start. Hmm...
I'm in a bit of a hurry, so i'll put the points up to 500.
Thank you,
John Gjonola
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the curious, I did a bit of extra research into it. It looks like what's happening is that, because the variable you created to store the Mutex never gets used after being initialized, the release version doesn't even generate a variable for it. It just creates it on the stack and lets it go away. Hence, a little while later (probably when the system comes under mild memory pressure from creating the Form), it gets GC'd, and when the next instance gets there, the mutex no longer exists.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys! Perfect...
John G.
John G.