473,398 Members | 2,113 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,398 software developers and data experts.

Mutex and single instance console app ?

PL

I simply cannot get this to work with my current project, if I create a test
project
with only the code below it works fine but in my real app it still allows
two instances.

using System;
using System.Threading;

namespace MutexTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Mutex appMutex = new Mutex(false, "Application");
if(appMutex.WaitOne(0, false))
{
System.Console.WriteLine("Running");
System.Console.ReadLine();
}
else
{
System.Console.WriteLine("App already running !");
}
}
}
}

The only difference between this test and my app is that I have more
code where I print the "Running" message, no other difference, still
it doesn't work !

I've read numerous other threads saying similar things but no solutions.
Could anyone shed some light as to why this does not work ?

Thank you
PL.
Nov 22 '05 #1
4 3751
PL

This is amazing, I solved it by putting the appMutex variable outside main
and make it static, now it works as expected.

I assume it's because somehow the mutex is garbage collected and therefore
released before it should be.

This seems like a bug to me, I would assume the compiler does some strange
optimization here since it seem to work with the local variable in debug
mode.

PL.

"PL" <pb****@yahoo.se> skrev i meddelandet
news:uC**************@TK2MSFTNGP12.phx.gbl...

I simply cannot get this to work with my current project, if I create a
test project
with only the code below it works fine but in my real app it still allows
two instances.

using System;
using System.Threading;

namespace MutexTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Mutex appMutex = new Mutex(false, "Application");
if(appMutex.WaitOne(0, false))
{
System.Console.WriteLine("Running");
System.Console.ReadLine();
}
else
{
System.Console.WriteLine("App already running !");
}
}
}
}

The only difference between this test and my app is that I have more
code where I print the "Running" message, no other difference, still
it doesn't work !

I've read numerous other threads saying similar things but no solutions.
Could anyone shed some light as to why this does not work ?

Thank you
PL.

Nov 22 '05 #2
No, this is not a bug in .NET, it's a "users bug". The JIT is free to mark
objects that are no longer referenced in the code path eligible for GC, the
fact that the underlying handle is a system wide kernel object that should
be kept for the duration of the program is not the JIT business.
To prevent premature collection of the Mutex you could wrap your code in a
using block (yes, Mutex derives from a Disposable), this is more elegant
than using a static.

using(System.Threading.Mutex m = new System.Threading.Mutex(true,
"YourNameHere"))
{
// Your code here
} // Done with the Mutex, the handle can be released

Willy.

"PL" <pb****@yahoo.se> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...

I simply cannot get this to work with my current project, if I create a
test project
with only the code below it works fine but in my real app it still allows
two instances.

using System;
using System.Threading;

namespace MutexTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Mutex appMutex = new Mutex(false, "Application");
if(appMutex.WaitOne(0, false))
{
System.Console.WriteLine("Running");
System.Console.ReadLine();
}
else
{
System.Console.WriteLine("App already running !");
}
}
}
}

The only difference between this test and my app is that I have more
code where I print the "Running" message, no other difference, still
it doesn't work !

I've read numerous other threads saying similar things but no solutions.
Could anyone shed some light as to why this does not work ?

Thank you
PL.

Nov 22 '05 #3
PL

Thank you, the using block worked fine as well and is as you say more
elegant.

I guess what confuses me here is the definition of "not referenced", if I
have a local
variable in Main that do not change you would think it stays referenced
during the
whole lifetime of the program but apparently not.

I guess I need to read up on the garbage collector.

Considering all the examples I found on the net on how to do this that
didn't use a static variable,
a using block or gc.keepalive it seems to me like only more memory intensive
programs experience
this issue which makes it somewhat dangerous.

PL.
"Willy Denoyette [MVP]" <wi*************@pandora.be> skrev i meddelandet
news:eH**************@TK2MSFTNGP15.phx.gbl...
No, this is not a bug in .NET, it's a "users bug". The JIT is free to mark
objects that are no longer referenced in the code path eligible for GC,
the fact that the underlying handle is a system wide kernel object that
should be kept for the duration of the program is not the JIT business.
To prevent premature collection of the Mutex you could wrap your code in a
using block (yes, Mutex derives from a Disposable), this is more elegant
than using a static.

using(System.Threading.Mutex m = new System.Threading.Mutex(true,
"YourNameHere"))
{
// Your code here
} // Done with the Mutex, the handle can be released

Willy.

Nov 22 '05 #4

"PL" <pb****@yahoo.se> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...

Thank you, the using block worked fine as well and is as you say more
elegant.

I guess what confuses me here is the definition of "not referenced", if I
have a local
variable in Main that do not change you would think it stays referenced
during the
whole lifetime of the program but apparently not.


This is something to watch out for. A local variable holding a reference to
an object in the managed heap, will be set to null by the JIT compiler
(running in non-debug mode) when it's no longer used in the scope of the
method. Now in the case of a Mutex, the underlying OS handle will only be
released when the finalizer comes along, which can be a long time after the
reference was null'ed. To take control of the lifetime of such an unmanaged
resource you should adopt the disposing pattern and wrap your code in a
using block, just like I've done with the using block.

Willy.
Nov 22 '05 #5

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

Similar topics

4
by: PL | last post by:
I simply cannot get this to work with my current project, if I create a test project with only the code below it works fine but in my real app it still allows two instances. using System;...
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
20
by: Michael A. Covington | last post by:
See: http://www.ai.uga.edu/mc/SingleInstance.html While attempting to use a mutex to allow only one instance of my app to run at a time (Recipe 4.12 in C# Programmer's Cookbook), I found that if...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
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()? ...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
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...
2
by: =?Utf-8?B?Q2F2ZXk=?= | last post by:
Hello, Encountering a issue where I define a mutex to enforce a single instance of an application. It works well under Debug but switching to release it fails everytime.. bool exclusive =...
2
by: cj | last post by:
I take it a mutex is like a synclock but visible outside the program?
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.