So I have the following code:
...
private static void Main(string[] args)
{
string file=DateTime.Now.ToFileTime().ToString();
File.AppendAllText(file, "Mutex\r\n");
bool CreatedNew;
Mutex mutex=new Mutex(true, AppDomain.CurrentDomain.FriendlyName, out CreatedNew);
if(CreatedNew)
{
#if DEBUG
File.AppendAllText(file, "Launching in DEBUG mode\r\n");
#else
File.AppendAllText(file, "Launching in RELEASE mode\r\n");
#endif
//Program.Launch();
Program.ProcessArgsAndLaunch(args);
}
else
{
File.AppendAllText(file, "Handling dupe\r\n");
Program.HandleDuplicate();
}
}
...
I've checked innumerable articles here and other sites with no luck.
Basically, the code checks for a running instance of the app and if there is one, it switches to the main window of the running one. If not, it launches the app.
In Debug
mode it all works as expected, the problem starts when I switch my configuration to Release
: the app always starts (with Mutex
seemingly doing nothing).
I've added conditionally compiled dumps that show in which mode the app is starting and the output changes based on the configuration, but sadly, so does the behaviour of the app.
This may be a race condition
but I am not sure.
More code will be posted if needed.
Thanks.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Along with Juan's answer, there's a difference in terms of garbage collection between launching inside and outside a debugger. That's not quite the same as a Debug configuration vs a Release configuration, but it's something you should be aware of anyway.
In the debugger, a local variable will act as a GC root for its entire scope - but when you're not debugging, your mutex
variable isn't a GC root at all, because you don't use it after initializing it. That means your Mutex
can be garbage collected (and thus the native mutex will be released) pretty much immediately.
You should use a using
statement to explicitly dispose the Mutex
at the right time:
// Note that you don't need a variable here... you can have one if you
// want though
using (new Mutex(...))
{
// Code here to be executed while holding the mutex
}
If you have one instance running as Debug and another as Release, they will not share the same mutex, because AppDomain.CurrentDomain.FriendlyName is different dependening on whether the hosting process is active or not.
As seen here: http://msdn.microsoft.com/en-us/library/ms242202.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With