Mutex does not stop two instances of program from running at once - c#

I am trying to use a Mutex to only allow one instance of my program to be run at a time. I reused the Mutex code from another program I was writing, only to find out that it did not stop two instances of my program from being run at once. However, my Mutex code worked in my other program. Below is the entire code of Program.cs (the code for opening files is unrelated). May you please explain how I should properly use the Mutex to prevent multiple instances of my program from being run at once? Thanks!
Note: my original code was based on this SO answer: https://stackoverflow.com/a/819808/12946280
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace NTCSAttendanceKiosk
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Make a mutex and detect if another instance of the program is running
Mutex mutex = new Mutex(true, "AtteNTCSKioskMutex", out bool mutexResult);
if (!mutexResult)
{
// Exit if it's already running
return;
}
// Prevent the mutex from being released by the GC
GC.KeepAlive(mutex);
// Read the connection string from the file
try
{
SqlConnectionInfo.ConnectionString = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\kiosk_config\\connection_string.txt");
}
catch (FileNotFoundException)
{
MessageBox.Show("The file connection_string.txt does not exist. Please place the connection string in that file and place it in <your user folder>\\kiosk_config\\. The kiosk program will now exit.", "Connection String File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
catch (IOException)
{
MessageBox.Show("File I/O error when loading connection_string.txt. The kiosk program will now exit.", "File I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Read the kiosk location name from the file
try
{
SqlConnectionInfo.KioskLocation = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\kiosk_config\\location.txt");
}
catch (FileNotFoundException)
{
MessageBox.Show("The file location.txt does not exist. Please place the kiosk location name in that file and place it in <your user folder>\\kiosk_config\\. The kiosk program will now exit.", "Connection String File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
catch (IOException)
{
MessageBox.Show("File I/O error when loading location.txt. The kiosk program will now exit.", "File I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Application.Run(new KioskForm());
}
}
}

You can test the below code by running several instances simultaneously.
static void Main(string[] args)
{
using (var mutex = new Mutex(true, "UniqueSystemWideMutexName"))
{
//Timeout is set to zero so we don't block
if (!mutex.WaitOne(0))
{
Console.WriteLine("Program already running");
Console.ReadKey();
return;
}
Console.WriteLine("This is the only program running");
Console.ReadKey();
}
}
If you can't use Dispose for whatever reason, which the using block does for us, be sure to call ReleaseMutex.
You can also use OpenExisting to check if the mutex has already been created, but it's not necessary for this simple use case.

Related

Start the same program many times but only one should work

Is it somehow possible that if I start my program like 10 times fast in a row, but only one at a time should do something. The other keep waiting that the working program is finished or stopped.
So in the end, if I open my program 10 times, all 10 programs should be working in a row, not simultaneously.
Is this possible in c#?
You can use a named EventWaitHandle to do this, for example:
using System;
using System.Threading;
namespace Demo
{
static class Program
{
static void Main()
{
using (var waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "MyHandleName"))
{
Console.WriteLine("Waiting for handle");
waitHandle.WaitOne();
try
{
// Body of program goes here.
Console.WriteLine("Waited for handle; press RETURN to exit program.");
Console.ReadLine();
}
finally
{
waitHandle.Set();
}
Console.WriteLine("Exiting program");
}
}
}
}
Try running a few instances of this console app and watch the output.
You can use system wide Mutex or system wide Semaphore. If you create Mutex or Semaphore with name it become visible for whole system - in other words it can be visible from other processes.
Mutex syncMutex = new Mutex(false, "NAME OF MUTEX");
try
{
if(!syncMutex.WaitOne(MUTEX_TIMEOUT))
{
//fail to get mutex
return;
}
//mutex obtained do something....
}
catch(Exception ex)
{
//handle error
}
finally
{
//release mutex
syncMutex.ReleaseMutex();
}

Kill other instances if program is running?

I am trying to make it so that if another instance of my program is running, it should close down the instance that's already running and start the new instance. I currently tried this:
[STAThread]
static void Main()
{
Mutex mutex = new System.Threading.Mutex(false, "supercooluniquemutex");
try
{
if (mutex.WaitOne(0, false))
{
// Run the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fMain());
}
else
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.Equals(Process.GetCurrentProcess().ProcessName) && proc.Id != Process.GetCurrentProcess().Id)
{
proc.Kill();
break;
}
}
// Run the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fMain());
}
}
finally
{
if (mutex != null)
{
mutex.Close();
mutex = null;
}
}
}
But for some reason it doesn't kill the already running instance, it just kills itself most of the time and sometimes it doesn't do anything at all.
What do I do to get this to work?
EDIT: I know the usual way of doing it is showing a message that the application is already running, but in this application it is vital that it kills the old process instead of showing a message.
First of all you need to wait for mutex again after killing the previous process. When you that you will get AbandonedMutexException. For details of that please check this link I am assuming it is OK to continue after that exception.
You can try .
Mutex mutex = new System.Threading.Mutex(false, "supercooluniquemutex");
try
{
bool tryAgain = true;
while (tryAgain)
{
bool result = false;
try
{
result = mutex.WaitOne(0, false);
}
catch (AbandonedMutexException ex)
{
// No action required
result = true;
}
if (result)
{
// Run the application
tryAgain = false;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fMain());
}
else
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.Equals(Process.GetCurrentProcess().ProcessName) && proc.Id != Process.GetCurrentProcess().Id)
{
proc.Kill();
break;
}
}
// Wait for process to close
Thread.Sleep(2000);
}
}
}
finally
{
if (mutex != null)
{
mutex.Close();
mutex = null;
}
}
In that example if I get AbandonedMutexException, I get ownership of the mutext and it is ok to continue.
Also, you use local Mutex, another user can run same application under another Terminal server session. MSDN says
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. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

Close executor app by executed app

I have got a problem with my C# project. I got there 2 applications:
Executor application which I will call Mini Launcher
Executed application which I will call Launcher
My problem is: I want to run my Launcher by Mini launcher and in on Show event of Launcher app close Mini Launcher.
My Mini Launcher is something like splash screen but with additional functionality like upgrade Launcher, and other. Its my execution code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "My Directory"
startInfo.FileName = "My App";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.();
}
}
catch
{
...
}
Have a look at the Mutex class. Named mutices provide a way for applications to send signals to one another.
The following sample shows two Console-applications. The TestMutexLauncher-application launches the TestMutex application:
using System;
using System.Diagnostics;
using System.Threading;
namespace TestMutexLauncher
{
class Program
{
static void Main(string[] args)
{
var p = Process.Start("TestMutex");
Console.WriteLine("Waiting for other process to release the mutex.");
Thread.Sleep(1000); // maybe p.WaitForInputIdle is an alternative for WinForms/WPF
Mutex mutex = null;
for (int i = 0; i < 100; i++)
{
if (Mutex.TryOpenExisting("MyUniqueMutexName", out mutex))
break;
Thread.Sleep(100);
}
if (mutex != null)
{
try
{
mutex.WaitOne();
mutex.ReleaseMutex();
}
finally
{
mutex.Dispose();
}
}
}
}
}
The launcher application starts the process and waits for a Mutex to be created in the other process. If it can acquire ownership of the the Mutex in a specified time frame, it waits to get ownership of the Mutex. After that, it realeases and disposes the Mutex.
The first task of the launched application is to create the Mutex, do the initialization actions and then release the Mutex.
using System;
using System.Threading;
namespace TestMutex
{
class Program
{
static void Main(string[] args)
{
using (var mutex = new Mutex(true, "MyUniqueMutexName"))
{
// Do something
for (int i = 0; i < 10000; i++)
Console.Write(".");
Console.WriteLine();
Console.WriteLine("Press enter...");
Console.ReadLine();
mutex.ReleaseMutex();
}
for (int i = 0; i < 10000; i++)
Console.Write(".");
Console.WriteLine();
Console.WriteLine("Press enter...");
Console.ReadLine();
}
}
}
Firstly, I would recommend you consider:
1) Do they actually need to be separate applications?
2) If so, why can't MiniLauncher just close itself after Launcher has loaded?
But if you have to do it this way, then the code you're looking for is something like this:
private void OnShow()
{
var target = Process.GetProcessesByName("MiniLauncher.exe").FirstOrDefault();
if (target != null)
{
// any other checks that this is indeed the process you're looking for
target.Close();
}
}
you can call another project executable from current running project and then you can close your application.
class Program
{
static void Main()
{
//
// Open the application "application" that is in the same directory as
// your .exe file you are running.
//
Process.Start("example.txt");
// or for another directory you need to specify full path
Process.Start("C:\\");
}
}

Only one program at a time

I have a situation where I need to have only one instance of a program running at the same time.
This would be trivial like this:
class OneAtATimePlease
{
static void Main()
{
// Naming a Mutex makes it available computer-wide. Use a name that's
// unique to your company and application (e.g., include your URL).
using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))
{
// Wait a few seconds if contended, in case another instance
// of the program is still in the process of shutting down.
if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
{
Console.WriteLine ("Another app instance is running. Bye!");
return;
}
RunProgram();
}
}
static void RunProgram()
{
Console.WriteLine ("Running. Press Enter to exit");
Console.ReadLine();
}
}
Except for the small detail, that I need the EXISTING process to terminate, not the new one.
I tried making a semaphore the existing process could listen to after grabbing the above mutex, but because I want it to wait for the semaphore then I can end up in a situation where the semaphore is always signaled and thus it doesn't work.
Anyone have a good idea on how to solve this problem?
You need inter-process communication, to send a signal to the existing application. For C#, see IPC Mechanisms in C# - Usage and Best Practices.
Cine, I've written a two-Mutex logic. The first one is the "execution lock", while the second is the "monitor lock".
When the first process can't acquire the monitor lock, it will exit and release the execution lock for the new process.
I'm not sure if this is the best solution, and any feedback will be welcome.
C#:
class Program
{
private static string processName = DateTime.Now.ToString("hh.mm.ss");
private static bool exitProcess;
private static Mutex firstLock
{
get
{
return new Mutex(false, "stackoverflow.com/questions/11304052/");
}
}
private static Mutex secondLock
{
get
{
return new Mutex(false, "stackoverflow.com/questions/11304052/ #2");
}
}
static void Main(string[] args)
{
Console.WriteLine(string.Format("Process {0} starting", processName));
exitProcess = false;
while (true)
{
using (firstLock)
{
Console.WriteLine(string.Format("Process {0} trying to get #1 mutex", processName));
if (!firstLock.WaitOne(TimeSpan.FromSeconds(1), false))
{
Console.WriteLine(string.Format("Process {0} #1 mutex in use, waiting for release", processName));
bool killFirstApp = false;
while (!killFirstApp)
{
killFirstApp = LockSecondMutex();
}
continue;
}
new Thread(MonitorSecondMutex).Start();
RunProgram();
firstLock.ReleaseMutex();
break;
}
}
}
static void RunProgram()
{
while (!exitProcess)
{
Console.WriteLine(string.Format("Process {0} running", processName));
Thread.Sleep(1000);
}
}
static void MonitorSecondMutex()
{
while (true)
{
using (secondLock)
{
if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
{
Console.WriteLine(string.Format("Process {0} lost second mutex. Will now exit.", processName));
exitProcess = true;
break;
}
secondLock.ReleaseMutex();
}
Thread.Sleep(500);
}
}
static bool LockSecondMutex()
{
while (true)
{
using (secondLock)
{
if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
{
continue;
}
Thread.Sleep(5000);
secondLock.ReleaseMutex();
}
return true;
}
}
}
You could possibly get this done by requesting access to some limited system-wide resource, such as port. Your application could bind a socket to a specific port on launch. If it fails to bind, send a termination signal to the running instance and try again.

Synchronisation of Two Threads

I have two threads in c#.. Now i need to wait for a particular statement to be executed before I can continue execution in the other thread which obviously is a case of synchronisation.
Is there any code that can carry this out as in using an in-built method?
This is the code example:
public void StartAccept()
{
try
{
newSock.BeginAccept(new AsyncCallback(Accepted), newSock);
}
catch (ArgumentException)
{
MessageBox.Show("Error in arguments while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (ObjectDisposedException)
{
MessageBox.Show("socket closed while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (SocketException)
{
MessageBox.Show("Error accessing socket while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (InvalidOperationException)
{
MessageBox.Show("Invalid operation while using begin-accept", "Error", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("Exception occurred while using begin-accept", "Error", MessageBoxButtons.OK);
}
}
This receives data from the desired host which is selected by the code:
private void listBox1_Click(object sender, EventArgs e)
{
String data = (String)this.listBox1.SelectedItem;
ip = Dns.GetHostAddresses(data);
clientIP = new IPEndPoint(ip[0], 5555);
newSock.Bind(clientIP);
newSock.Listen(100);
}
So in order to start receiving data I need to initialise the socket to the particular remote host which is done when i click on one of the hosts shown in the listbox.
For this I need the synchronization.
Take a look at AutoResetEvent and ManualResetEvent. They are signals that makes synchronisation between threads possible.
The first thread that needs to wait for something to get done will do myEvent.WaitOne(), which blocks until the other thread calls myEvent.Set().
Let's say we have two threads, where one of them needs to do some kind of initialisation before the other thread can continue. You then share a AutoResetEvent between the two, let's call it myEvent.
// Signal example
using System;
using System.Threading;
class MySync
{
private readonly AutoResetEvent _myEvent;
public MySync(AutoResetEvent myEvent)
{
_myEvent = myEvent;
}
public void ThreadMain(object state)
{
Console.WriteLine("Starting thread MySync");
_myEvent.WaitOne();
Console.WriteLine("Finishing thread MySync");
}
}
class Program
{
static void Main(string[] args)
{
AutoResetEvent myEvent = new AutoResetEvent(false);
MySync mySync = new MySync(myEvent);
ThreadPool.QueueUserWorkItem(mySync.ThreadMain);
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
myEvent.Set();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
Console.WriteLine("Finishing");
}
}
Don't confuse this with a shared resource where the access order doesn't matter. For example, if you have a shared list or a shared dictionary you need to wrap it in a mutex in order to guarantee that they execute correctly.
// Mutex example
object mySync = new object();
Dictionary<int, int> myDict = new Dictionary<int, int>();
void threadMainA()
{
lock(mySync)
{
mySync[foo] = bar;
}
}
void threadMainB()
{
lock(mySync)
{
mySync[bar] = foo;
}
}
You can use an AutoResetEvent.
In the following example two methods get called by different threads and DoSomethingA() will be executed and finish before DoSomethingB() starts:
AutoResetEvent resetEvent = new AutoResetEvent(false);
void ThreadWorkerA()
{
// perform some work
DoSomethingA();
// signal the other thread
resetEvent.Set();
}
void ThreadWorkerB()
{
// wait for the signal
resetEvent.WaitOne();
// perform the new work
DoSomethingB();
}
Note: remember to dispose the AutoResetEvent :)
Java has something called Join. I suspect there will be a predefined method in C# too.

Categories

Resources