S
Steve Barnett
I've tried to create a "single instance" application using a mutex and, as
far as I can see, it's functioning right up until I close the application.
When I try to release the mutex, I get an ApplicationException with the
explanation: "Object synchronisation method was called from an
unsynchronised block of code".
I'm sure this tells me exactly how to fix it... well, it would if I
understood it. Can anyone suggest where I'm going wrong? (By the way, if I
don't release the Mutex, everything works perfectly well - I'm just trying
to do things properly).
I've tried surrounding the code to obtain and release the Mutex with a
lock() statement, but that made no difference. The code is shown below...
Thanks
Steve
namespace SingleInstance
{
static class Program
{
private static Mutex appMutex = null;
[STAThread]
static void Main()
{
bool newMutexCreated = false;
string mutexName = "Local\\IPM4Mutex";
try
{
appMutex = new Mutex(false, mutexName, out
newMutexCreated);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
}
if (newMutexCreated == true)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
// We're not the first instalce of this program. Tell the
other instance
// what our command line was...
SingleAppServices SendCommandLine = new SingleAppServices();
SendCommandLine.Channels.Add("CommandLineParms");
SendCommandLine.Channels["CommandLineParms"].Send(String.Join("
", System.Environment.GetCommandLineArgs()));
// Now close this form - we don't need it any more
Application.Exit();
}
GC.KeepAlive(appMutex);
// ********************** This is where
// ********************** I get the exception.
appMutex.ReleaseMutex();
}
}
}
far as I can see, it's functioning right up until I close the application.
When I try to release the mutex, I get an ApplicationException with the
explanation: "Object synchronisation method was called from an
unsynchronised block of code".
I'm sure this tells me exactly how to fix it... well, it would if I
understood it. Can anyone suggest where I'm going wrong? (By the way, if I
don't release the Mutex, everything works perfectly well - I'm just trying
to do things properly).
I've tried surrounding the code to obtain and release the Mutex with a
lock() statement, but that made no difference. The code is shown below...
Thanks
Steve
namespace SingleInstance
{
static class Program
{
private static Mutex appMutex = null;
[STAThread]
static void Main()
{
bool newMutexCreated = false;
string mutexName = "Local\\IPM4Mutex";
try
{
appMutex = new Mutex(false, mutexName, out
newMutexCreated);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
}
if (newMutexCreated == true)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
// We're not the first instalce of this program. Tell the
other instance
// what our command line was...
SingleAppServices SendCommandLine = new SingleAppServices();
SendCommandLine.Channels.Add("CommandLineParms");
SendCommandLine.Channels["CommandLineParms"].Send(String.Join("
", System.Environment.GetCommandLineArgs()));
// Now close this form - we don't need it any more
Application.Exit();
}
GC.KeepAlive(appMutex);
// ********************** This is where
// ********************** I get the exception.
appMutex.ReleaseMutex();
}
}
}