Preventing an app from being launched more than once

  • Thread starter Thread starter Mark Rae
  • Start date Start date
Mark Rae said:
Well, that's just a snippet - could you show the whole code, in the
form of a short but complete program?

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

bool blnFirstInstance;
Mutex objMutex = new Mutex(false, "Local\\" + "abcdefg", out
blnFirstInstance);
if (!blnFirstInstance)
{
MessageBox.Show("App is already running", "Single Use Only",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}

// rest of Main() code
}

Works perfectly on 64-bit Vista Business, doesn't work on 32-bit
WinXPPro+SP2 - haven't tried on any other version of Windows...

As others have said, the "using" statement is a good way to go. Another
alternative is to use GC.KeepAlive (as suggested on the web page, in
fact). It's very odd if that fails, even in a VPC.
 
As others have said, the "using" statement is a good way to go.

Still doesn't help in XP in a virtual machine...
Another alternative is to use GC.KeepAlive (as suggested on the web page,
in
fact).

OK - I'll have a try.

It's very odd if that fails, even in a VPC.

I couldn't agree more! But it does, and not on just one VM...
 

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

Back
Top