Mutex...

V

VJ

I hope this is right forum for this...

I have the below block of code in my Main. When I compile in debug mode, I
get to run one instance only. When I compile in release mode, I get to run
more than one instance of my application. These are the first lines of my
application.. I tried to set the mutexName to a string or a GUID or the full
assembly name, still I can't get single Instance behaviour in Release mode

bool mutexWasCreated = false;
bool requestInitialOwnership = true;
string mutexName = "MyApp";
//System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Reflection.Assembly.GetExecutingAssembly()).ToString();
//System.Reflection.Assembly.GetExecutingAssembly().FullName;

System.Threading.Mutex m = new
Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);

// If I put a log information here. I get True also..

try
{
if (mutexWasCreated)
{
m.ReleaseMutex();
}
else
{
MessageBox.Show("Application is already
running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
finally
{
m.Close();
}

// Code here calls Application.Run
 
G

Guest

This works for me. include the system.diagnostics namespace and here is my
code for the Main function:

static void Main()
{
string appName =
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Process[] pi = Process.GetProcessesByName(appName);

if (pi.GetUpperBound(0) > 0)
{
MessageBox.Show("This application is already running.");

Application.ExitThread();
Application.Exit();
}
else
{
Application.Run(new Form1());
}
}

Hope that helps.

Thanks.
 
B

Brian Richards

VJ said:
I hope this is right forum for this...

I have the below block of code in my Main. When I compile in debug mode, I
get to run one instance only. When I compile in release mode, I get to run
more than one instance of my application. These are the first lines of my
application.. I tried to set the mutexName to a string or a GUID or the full
assembly name, still I can't get single Instance behaviour in Release mode

bool mutexWasCreated = false;
bool requestInitialOwnership = true;
string mutexName = "MyApp";
//System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Re
flection.Assembly.GetExecutingAssembly()).ToString();
//System.Reflection.Assembly.GetExecutingAssembly().FullName;

System.Threading.Mutex m = new
Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);

// If I put a log information here. I get True also..

try
{
if (mutexWasCreated)
{
m.ReleaseMutex();
}
else
{
MessageBox.Show("Application is already
running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
finally
{
m.Close();
}

// Code here calls Application.Run


I've been succuessful this way

bool owner = false;
Mutex mutex = new Mutex(true, "Global\\" +
Assembly.GetExecutingAssembly().FullName, out owner);

if (owner)
{
AppFoo foo = new AppFoo();
try
{
//Start Application
Application.Run(foo.MainForm);
}
finally
{
mutex.ReleaseMutex();
}
}
else
{
//Show existing Application
IntPtr hWnd = NativeMethods.FindWindow(Application.ProductName);
if (hWnd != IntPtr.Zero)
{
NativeMethods.ShowWindow(hWnd, NativeMethods.ShowCmd.Restore );
NativeMethods.SetForegroundWindow(hWnd);
}
}
 

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