Mutex run ok in debug model,but failed in release model

G

Guest

I use this code to ensure only one application is running at one time.
In debug version, it runs ok
But, in release version, it failed. I don't why???
bool flgDouble ;
Mutex m = new Mutex( true, "App", out flgDouble);
if(flgDouble)
{
//Run Application
RunApp();
}
 
J

Jon Skeet [C# MVP]

Napo said:
I use this code to ensure only one application is running at one time.
In debug version, it runs ok
But, in release version, it failed. I don't why???
bool flgDouble ;
Mutex m = new Mutex( true, "App", out flgDouble);
if(flgDouble)
{
//Run Application
RunApp();
}

Chances are it's being garbage collected. Either stick it in a "using"
statement, or use a GC.KeepAlive(m); statement after your RunApp().
 
C

cody

I use this code to ensure only one application is running at one time.
Chances are it's being garbage collected. Either stick it in a "using"
statement, or use a GC.KeepAlive(m); statement after your RunApp().

But disposing it wouldn't let it stay alive, does it?
 
J

Jon Skeet [C# MVP]

cody said:
But disposing it wouldn't let it stay alive, does it?

If you put the whole thing in a using statement, including the RunApp
part, then the Dispose call wouldn't happen until after the app had
run. Sorry if I wasn't clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top