473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ReleaseMutex problem

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(fals e);
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();

}
}
}
Aug 8 '06 #1
5 6009
Ok, I found it.

Steve

"Steve Barnett" <no****@nodomain.comwrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
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(fals e);
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();

}
}
}


Aug 8 '06 #2
Steve,

If you are using .NET 2.0, there is a much simpler solution available to
you. I posted a response about it a while ago, and you can see it here:

http://groups.google.com/group/micro...128f9ddd957ada

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.comwrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
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(fals e);
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();

}
}
}


Aug 8 '06 #3
I remembered your response and I tried using WindowsFormsApplicationBase but
it invariably failed with an access violation.

I even tried a sample app I downloaded from CodeProject and that failed with
the same error. I then downloaded a VB.Net app that used this mechanism (on
the basis that it might be an issue between C# and VB) and, guess what, it
had an access violation. I've looked around the net a little and it seems
that there is some sort of problem with the socket libraries and how they
interact with other applications (non-specific but it seems Norton AV is an
issue).

So I gave up on the basis that it's unstable and not worth the risk. I'm now
using a Mutex and some code I found on vbaccelerator.com that allows me to
send data to another app via the WM_COPYDATA message. As it turned out, this
is also useful for sending data within the app - now all I need is a reason
want to do that.

Thanks
Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eP**************@TK2MSFTNGP06.phx.gbl...
Steve,

If you are using .NET 2.0, there is a much simpler solution available
to you. I posted a response about it a while ago, and you can see it
here:

http://groups.google.com/group/micro...128f9ddd957ada

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.comwrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
>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(fals e);
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();

}
}
}



Aug 8 '06 #4
Steve,

Can you show the code, and where the access violation takes place? You
are right that it is most likely the AV, but that does not mean that the
code is unstable, as it is not the responsibility of
WindowsFormsApplicationBase to be aware of AV, but the other way around.

And AFAIK, it doesn't use sockets but a named pipe for communication
between processes (it would be overkill to use sockets).

i

"Steve Barnett" <no****@nodomain.comwrote in message
news:Om**************@TK2MSFTNGP05.phx.gbl...
>I remembered your response and I tried using WindowsFormsApplicationBase
but it invariably failed with an access violation.

I even tried a sample app I downloaded from CodeProject and that failed
with the same error. I then downloaded a VB.Net app that used this
mechanism (on the basis that it might be an issue between C# and VB) and,
guess what, it had an access violation. I've looked around the net a
little and it seems that there is some sort of problem with the socket
libraries and how they interact with other applications (non-specific but
it seems Norton AV is an issue).

So I gave up on the basis that it's unstable and not worth the risk. I'm
now using a Mutex and some code I found on vbaccelerator.com that allows
me to send data to another app via the WM_COPYDATA message. As it turned
out, this is also useful for sending data within the app - now all I need
is a reason want to do that.

Thanks
Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eP**************@TK2MSFTNGP06.phx.gbl...
>Steve,

If you are using .NET 2.0, there is a much simpler solution available
to you. I posted a response about it a while ago, and you can see it
here:

http://groups.google.com/group/micro...128f9ddd957ada

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.comwrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
>>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(fals e);
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();

}
}
}




Aug 8 '06 #5
I took my code from http://www.codeproject.com/csharp/CSSIApp.asp. This was
perfect for what I wanted.

When my code didn't work, I compiled the sample app that the author provided
and still got the access violation. The error itself appears in VS but is
not associated with any specific line of code (i.e. no line is highlighted).
I tried a Try/Catch around the code and it did NOT catch the error.

The specific error you get is:
" at
System.Net.UnsafeNclNativeMethods.OSSOCK.WSAGetOve rlappedResult(SafeCloseSocket
socketHandle, IntPtr overlapped, UInt32& bytesTransferred, Boolean wait,
IntPtr ignored)\r\n at
System.Net.Sockets.BaseOverlappedAsyncResult.Compl etionPortCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)\r\n at
System.Threading._IOCompletionCallback.PerformIOCo mpletionCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)"

As you'll see from the comments at the bottom of the article, someone else
has had the same problem but no solution was offered by the author. Another
poster suggested the problem was NetLimiter, but I don;'t have that running.
When I checked through Google, I found similar issues, one of which
suggested NAV as the culprit.

I guess the point is that I can't control what my end users will run ion
their PCs and I certainly can't tell them that they must not run specific
packages.

I don't know whether this is a failing of the .Net framework or something
else. All I know is that it does not work on my development machine, so I
can't legitimately give it to a user.

Thanks
Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Oz**************@TK2MSFTNGP06.phx.gbl...
Steve,

Can you show the code, and where the access violation takes place? You
are right that it is most likely the AV, but that does not mean that the
code is unstable, as it is not the responsibility of
WindowsFormsApplicationBase to be aware of AV, but the other way around.

And AFAIK, it doesn't use sockets but a named pipe for communication
between processes (it would be overkill to use sockets).

i

"Steve Barnett" <no****@nodomain.comwrote in message
news:Om**************@TK2MSFTNGP05.phx.gbl...
>>I remembered your response and I tried using WindowsFormsApplicationBase
but it invariably failed with an access violation.

I even tried a sample app I downloaded from CodeProject and that failed
with the same error. I then downloaded a VB.Net app that used this
mechanism (on the basis that it might be an issue between C# and VB) and,
guess what, it had an access violation. I've looked around the net a
little and it seems that there is some sort of problem with the socket
libraries and how they interact with other applications (non-specific but
it seems Norton AV is an issue).

So I gave up on the basis that it's unstable and not worth the risk. I'm
now using a Mutex and some code I found on vbaccelerator.com that allows
me to send data to another app via the WM_COPYDATA message. As it turned
out, this is also useful for sending data within the app - now all I need
is a reason want to do that.

Thanks
Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eP**************@TK2MSFTNGP06.phx.gbl...
>>Steve,

If you are using .NET 2.0, there is a much simpler solution available
to you. I posted a response about it a while ago, and you can see it
here:

http://groups.google.com/group/micro...128f9ddd957ada

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Barnett" <no****@nodomain.comwrote in message
news:un**************@TK2MSFTNGP02.phx.gbl...
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(fals e);
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();

}
}
}




Aug 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
5
by: ste | last post by:
this code not work: it is no possible Exit from Monitor Why ? thank ste //////// using System; using System.Collections;
11
by: Tyler Sample | last post by:
I've been having strange thread conflicts, and after moving the relevant mutex higher and higher I've finally determined that the mutex doesn't seem to be working at all. Here is the relevant...
3
by: ano | last post by:
How to reset Mutex.ReleaseMutex()? I can't call Mutex.WaitOne() again because the Mutex status seem to be fix to ReleaseMutex(). Do I need to create new Mutex() every time after ReleaseMutex()? ...
0
by: Chris Saunders | last post by:
I'm using System.ComponentModel.BackgroundWorker to do some drawing in my application. The thread seems to run fine but it runs twice and I can't see why. I'm going to show my code with everything...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
1
by: cold80 | last post by:
I'm trying to check in my application if another instance of it is already running. I found many code snippets on the net that make use of a named mutex to do this check, but I can't make it work...
8
by: Dan Pavel | last post by:
Hi, I did not used Mutex before and I need now. I have an application that control some workflows on different machines. I use SNMP for that. The problem is that the thread used for starting an...
2
by: r3d dra6on | last post by:
I am not sure where I am going wrong with this. I get a lot of LINK errors, and I am unsure of how to solve the problem with them. Can someone please tell me where I went wrong? #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.