How to avoid race condition when acquiring a mutex? - c#

I am using a named system mutex to synchronise 2 processes. This is how I am currently acquiring a mutex within my application:
using System.Threading;
public static bool AcquireMutex()
{
// Protect against double acquisitions
if (MyMutex != null)
{
throw new ApplicationException("Failed to acquire mutex");
}
try
{
// See if a named system mutex has already been created - if it has,
// wait a short amount of time for its release.
MyMutex = Mutex.OpenExisting(MutexName);
if (!MyMutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
// MyMutex still being held
MyMutex = null;
return false;
}
}
catch
{
// MyMutex doesn't exist so create it
MyMutex = new Mutex(true, MutexName);
}
return true;
}
OpenExisting will throw an exception if the named system mutex with MutexName doesn't exist, allowing my application to create it.
However, there seems to be a race condition here - if OpenExisting throws, there is a small window before the call to new Mutex where the other application may have acquired the mutex.
What is the best way to avoid this race condition and make this code more reliable?
A colleague mentioned that he was using CreateMutex from the Win32 Platform SDK in his code (the other process which needs to be synchronised). This doesn't seem to be natively supported by the .NET Framework, however. So I'm not sure it's the best solution for my code.
Update
Based on the answer from #David Schwartz, here is my new code:
public static bool AcquireMutex()
{
// Protect against double acquisitions
if (MyMutex != null)
{
throw new ApplicationException("Failed to acquire mutex");
}
bool createdNew;
MyMutex = new Mutex(true, MutexName, out createdNew);
if (createdNew)
{
// Mutex was created so ownership is guaranteed; no need to wait on it.
return true;
}
try
{
if (!MyMutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
MyMutex = null;
return false;
}
}
catch (AbandonedMutexException)
{
// Other application was aborted, which led to an abandoned mutex.
// This is fine, as we have still successfully acquired the mutex.
}
return true;
}

There's a constructor specifically designed for this purpose. From the docs:
createdNew
Type: System.Boolean
When this method returns, contains a Boolean that is true if a local mutex was created (that is, if name is null or an empty string) or if the specified named system mutex was created; false if the specified named system mutex already existed. This parameter is passed uninitialized.

Related

Mutex opened in Delphi application checked in C# console application

I'm launching a Delphi app and create a mutex for it like this:
var
AMutex: THandle;
function OpenMutex(const AMutexName: String): Boolean;
begin
{ Assume the Windows Mutext is already open }
Result := False;
{ Is the Mutex already open? }
if AMutex <> 0 then
exit;
{ Try to create Windows Mutex }
if CreateProgramMutex( AMutexName , AMutex) then
Result := True
else
AMutex := 0;
end;
function CreateProgramMutex( AMutexName: string; var AMutex: THandle ): boolean;
begin
{ Assume the new program mutex was created successfully. }
Result := true;
{ Attempt to create a new mutex. }
AMutex := CreateMutex(nil, False, PChar(AMutexName));
{ If we at least got a handle to the mutex... }
if (AMutex <> 0) then
begin
if GetLastError = ERROR_ALREADY_EXISTS then begin
{ Close the handle, since it already exists. }
CloseHandle(AMutex);
{ Set the return to show that it was already running. }
Result := false;
end;
end else
Result := false;
end;
And I'm trying from C#(as a beginner) to find out if my application is already running in a console app:
using System;
using System.Threading;
namespace ConsoleApplication1
{
class OneAtATimePlease
{
private static Mutex _mutex;
private static bool IsSingleInstance()
{
_mutex = new Mutex(false, "my mutex name");
// keep the mutex reference alive until the normal
//termination of the program
GC.KeepAlive(_mutex);
try
{
return _mutex.WaitOne(0, false);
}
catch (AbandonedMutexException)
{
// if one thread acquires a Mutex object
//that another thread has abandoned
//by exiting without releasing it
_mutex.ReleaseMutex();
return _mutex.WaitOne(0, false);
}
}
static void Main()
{
if (!IsSingleInstance())
Console.WriteLine("already running");
Console.ReadLine();
}
}
}
Even the Delphi app is running, IsSingleInstance is returning true. Checking the mutex in a Delphi console app using the same Delphi code is working. I'm sure that it's something obvious but I couldn't figure out what I'm doing wrong.
PS: Everything is done under the same Windows user session
You say that your aim is to check if a foreign application is running (by using a named mutex). Well, for such case you should not attempt to create a mutex object of the given name in your application but only try to open such. The reason is simple, if that foreign application uses such mutex to check if it is running by itself, you would actually steal this mutex for your application, and that foreign one would never start.
For your purpose use the TryOpenExisting class function. For example:
using System;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Mutex mutex;
if (Mutex.TryOpenExisting("My unique mutex name", out mutex)) {
try {
// with the used TryOpenExisting overload you can work with
// the mutex object here; you can wait for it or release
Console.WriteLine("Application is running!");
}
finally {
mutex.Close();
}
}
else {
Console.WriteLine("Application is NOT running!");
}
Console.ReadLine();
}
}
}
I think you need to check if the mutex was exsiting or created.
Mutex appMutex = new Mutex(true, "MyMutex", out exclusive);
if (!exclusive)
{
//Instance already existed
}

Mutex kills application

This is my code:
protected override async void OnStartup(StartupEventArgs e)
{
// Used to check if we can create a new mutex
bool newMutexCreated = false;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false, MutexName, out newMutexCreated);
}
catch (Exception ex)
when (ex is UnauthorizedAccessException ||
ex is IOException ||
ex is WaitHandleCannotBeOpenedException ||
ex is ArgumentException)
{
Logger.Error("Error while launching application. Failed to check for other instances.", ex);
Shutdown((int)ExitCode.ApplicationAlreadyRunning);
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if (newMutexCreated)
{
await ContinueStartup(e);
return;
}
else
{
// Otherwise we get the first instance with that process name,
Process[] currentProcesses = Process.GetProcessesByName(AssemblyName);
IntPtr mainWindowHandle = currentProcesses[0].MainWindowHandle;
if (mainWindowHandle != IntPtr.Zero)
{
// maximize it, if it was minimized, and set it to foreground.
Logger.Info("Another instance of the application is already running.");
ShowWindow(mainWindowHandle, WindowShowNormal);
SetForegroundWindow(mainWindowHandle);
}
// Then shutdown this instance.
Logger.Info("Shutting down.");
Shutdown((int)ConsoleModeExitCode.ApplicationAlreadyRunning);
}
}
protected override void OnExit(ExitEventArgs e)
{
Logger.Info("Exiting application.");
// Close mutex.
mutex.Dispose();
base.OnExit(e);
}
What happens here is that my application should start once. While it is running, every attempt to start a new instance should bring the first instance to the front.
But what actually happens is: after 2-10 launch-attempts the first instance's GUI is killed, the process is still running and blocking the Mutex and can only be killed in the TaskManager. If I try to debug this behaviour and run the application in VisualStudio, it just never happens. Trying to open the application 50 times never kills it, so I can't follow the events that seem to occur.
Is it normal behaviour of the GarbageCollector? It kills the first instance in case it is hanging?
Or am I missing something?
Okay, as #Luaan mentioned the problem was not the Mutex.
I fixed my code with this solution:
https://stackoverflow.com/a/9059657/3319147
ShowWindowAsync and a slightly different handling of the IntPtr-value of the handle seems to make this way more stable. Couldn't crash it since. For me this is enough stability :)

Change child process's parent

i was searching how to do it for about 6 hours,but didn't find a way.
Is there any way i can change a process's parent process? some api maby ?
google didn't gave much, same for this site, so i opened new question.
What i'm trying to do is to lock a file for personal use, then delete it.
i create the file on program A and use it with program B, when B finish the use, i delete with A, the thing is that B creates a sub process, which don't have B as his parent, so when i use :
File.Open(_moviePath, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
I try to lock the file because i don't want other programs/users to be able to copy it but
it failes.
tnx.
instead of locking the file this way, why not use Mutex? It allows for cross process locking. This will work fine if this is to remain on a single box. http://msdn.microsoft.com/en-us/library/bwe34f1k(v=vs.110).aspx
And no you cannot reassign a parent process owner to a child process.
Here is an example, i will explain below: http://www.dotnetperls.com/mutex
using System;
using System.Threading;
class Program
{
static Mutex _m;
static bool IsMutexExisting(string token)
{
try
{
// Try to open existing mutex.
Mutex.OpenExisting(token);
}
catch
{
return true;
}
// More than one instance.
return false;
}
So in your example program A will do it's thing and then wait.. how to get A to wait?
Have program A attempt to open an existing mutex (a mutex that only B will create), for example... pcode:
while( IsMutexExisting("B Token") == false )
{
System.Threading.Thread.Sleep(500); //sleep for a 1/2 sec
}
//ok, B has created the mutex, let's wait for it to be released indicating it is complete.
Mutex m = Mutex.OpenExisting("B Token");
m.WaitOne(); // will block execution until B releases the Mutex
// lock created, this means B signaled us
// do the rest of A code here...
Program B:
<does what it does>
//Create Mutex to signal A
Mutex m = null;
try{
m =new Mutex(true,"B Token");
...
...
}
finally{
m.ReleaseMutex();
}

How to make my app singleton application? [duplicate]

This question already has answers here:
What is the correct way to create a single-instance WPF application?
(39 answers)
Closed 6 years ago.
I have a application but currently it is not a singleton application.
I like to make it singleton application so that its another instance does not exit at the run time .
If this can be done please reply with some sample codes .
I think the following codes will be helpful for you.
Here is the related link:
http://geekswithblogs.net/chrisfalter/archive/2008/06/06/how-to-create-a-windows-form-singleton.aspx
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/*====================================================
*
* Add codes here to set the Winform as Singleton
*
* ==================================================*/
bool mutexIsAvailable = false;
Mutex mutex = null;
try
{
mutex = new Mutex(true, "SampleOfSingletonWinForm.Singleton");
mutexIsAvailable = mutex.WaitOne(1, false); // Wait only 1 ms
}
catch (AbandonedMutexException)
{
// don't worry about the abandonment;
// the mutex only guards app instantiation
mutexIsAvailable = true;
}
if (mutexIsAvailable)
{
try
{
Application.Run(new SampleOfSingletonWinForm());
}
finally
{
mutex.ReleaseMutex();
}
}
//Application.Run(new SampleOfSingletonWinForm());
}
}
Here are some good sample applications. Below is one possible way.
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.
Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
if (MainForm.RunningInstance() != null)
{
MessageBox.Show("Duplicate Instance");
//TODO:
//Your application logic for duplicate
//instances would go here.
}
Many other possible ways. See the examples for alternatives.
First one.
Second One.
Third One.
The approach I know of is the following. The program must attempt to open a named mutex. If that mutex existed, then exit, otherwise, create the mutex. But this seems to contradict your condition that "its another instance does not exit at the run time". Anyway, maybe this too was helpful

How can I check for a running process per user session?

I have a .NET application that I only allow to run a single process at a time of, however that app is used on Citrix boxes from time to time, and as such, can be run by multiple users on the same machine.
I want to check and make sure that the application is only running once per user session, because right now if user A is running the app, then user B gets the "App already in use" message, and should not.
This is what I have now that checks for the running process:
Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (p.Length > 1)
{
#if !DEBUG
allowedToOpen &= false;
errorMessage +=
string.Format("{0} is already running.{1}", Constants.AssemblyTitle, Environment.NewLine);
#endif
}
EDIT: Improved the answer according to this cw question ...
You can use a mutex for checking wether the app already runs:
using( var mutex = new Mutex( false, AppGuid ) )
{
try
{
try
{
if( !mutex.WaitOne( 0, false ) )
{
MessageBox.Show( "Another instance is already running." );
return;
}
}
catch( AbandonedMutexException )
{
// Log the fact the mutex was abandoned in another process,
// it will still get aquired
}
Application.Run(new Form1());
}
finally
{
mutex.ReleaseMutex();
}
}
Important is the AppGuid - you could make it depend on the user.
Maybe you like to read this article: the misunderstood mutex
As tanascius already say, you can use the Mutex.
On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created.
Source: msdn, Mutex Class
Just stating the obvious - although Mutex is usually considered better solution, you can still solve the single-instance-per-session issue without Mutex - just test the SessionId as well.
private static bool ApplicationIsAlreadyRunning()
{
var currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(currentProcess.ProcessName);
// test if there's another process running in current session.
var intTotalRunningInCurrentSession = processes.Count(prc => prc.SessionId == currentProcess.SessionId);
return intTotalRunningInCurrentSession > 1;
}
Source (no Linq)
If Form1 launches non-background threads, and that Form1 exits, you've got a problem: the mutex is released but the process is still there. Something along the lines below is better IMHO:
static class Program {
private static Mutex mutex;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
bool createdNew = true;
mutex = new Mutex(true, #"Global\Test", out createdNew);
if (createdNew) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else {
MessageBox.Show(
"Application is already running",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
}
The mutex won't be released as long as the primary application domain is still up. And that will be around as long as the application is running.

Categories