Only one instance of program running at a time.

  • Thread starter Thread starter msnews.microsoft.com
  • Start date Start date
M

msnews.microsoft.com

What's the easiest way to make a program so that only one instance is ever
running at a time?

I'm doing a mutex at the moment - problem is - when the main window closes,
I try and release the mutex and I'm getting a 'object synchronization method
was called from an unsynchronized block of code.'

I've seen things where you write to the registry but my fear is that if the
program doesn't shut down normally it means it can never run again.

TIA - Jeff.
 
What's the easiest way to make a program so that only one instance is ever
running at a time?

I'm doing a mutex at the moment - problem is - when the main window closes,
I try and release the mutex and I'm getting a 'object synchronization method
was called from an unsynchronized block of code.'

I've seen things where you write to the registry but my fear is that if the
program doesn't shut down normally it means it can never run again.

TIA - Jeff.

I don't know if it was trhe easist way to do it but I have done this
in .NET with a mutex.

In Program.cs:

bool bOK;
System.Threading.Mutex m;
m = new System.Threading.Mutex(true, "assemblyname", out bOK);
if (bOK)
{
Application.Run(<main form>);
}
else
{
<display an appropriate message that only one instance can be
running at the time>
}
GC.KeepAlive(m);
 
How and when do you dispose of the mutex?

J.

What's the easiest way to make a program so that only one instance is ever
running at a time?

I'm doing a mutex at the moment - problem is - when the main window
closes,
I try and release the mutex and I'm getting a 'object synchronization
method
was called from an unsynchronized block of code.'

I've seen things where you write to the registry but my fear is that if
the
program doesn't shut down normally it means it can never run again.

TIA - Jeff.

I don't know if it was trhe easist way to do it but I have done this
in .NET with a mutex.

In Program.cs:

bool bOK;
System.Threading.Mutex m;
m = new System.Threading.Mutex(true, "assemblyname", out bOK);
if (bOK)
{
Application.Run(<main form>);
}
else
{
<display an appropriate message that only one instance can be
running at the time>
}
GC.KeepAlive(m);
 
Thanks for the help. The problem is (at I said before) I create the mutex on
the form startup. When the event Form Closing is called and I try to release
the mutex, I get an 'object synchronization method was called from an
unsynchronized block of code' in the event.

So what I'm trying to find out is if I can't release the mutex in
unsynchronized code, how do I synchronize it or what do I do to make it so
that I can release the mutex.

TIA - Jeff.
 
Will this automatically release the mutex?

Mark Rae said:
using System.Threading;

bool bOK;
using (Mutex m = new Mutex(true, "Local\\" + "assemblyname", out bOK))
{
if (bOK)
{
Application.Run(<main form>);
}
else
{
// message
}
}
 
That's what the using() statement does, it calls Dispose() on the mutex you
create.
 
Thanks for the help. The problem is (at I said before) I create the mutex on
the form startup. When the event Form Closing is called and I try to release
the mutex, I get an 'object synchronization method was called from an
unsynchronized block of code' in the event.

Why would you release the mutex? Your process is about to be stopped
anyway, and Windows will release any kernel objects it still holds.

The pattern that I've used to create a single-instance application is
shown in all details here:
http://kristofverbiest.blogspot.com/2008/11/creating-single-instance-application.html
 
Back
Top