Only one instance of program running at a time.

  • Thread starter msnews.microsoft.com
  • 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.
 
Z

zacks

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);
 
M

msnews.microsoft.com

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);
 
Z

zacks

How and when do you dispose of the mutex?

I don't. What you see in the sample code is it. I "stole" this code
from someone else and it appears to work fine.
 
M

Mufasa

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.
 
M

Mufasa

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
}
}
 
P

Peter Morris

That's what the using() statement does, it calls Dispose() on the mutex you
create.
 
V

verbiest

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
 

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