+ 2

How to allow one instance of exe

Hi I have an application which need to have only one instance of the application active at a time. What to do when we have to restrict one instance of exe is active?

14th Feb 2025, 5:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
9 Réponses
+ 3
I have done this in Windows via system API calls to get the list of running windows and see if your window title is in the list. I always felt that it needed to be more reliable than using the Title property, but it was the best idea I could find. It solved the problem and there were no reports that indicated otherwise.
14th Feb 2025, 11:16 PM
Brian
Brian - avatar
+ 2
..
15th Feb 2025, 6:20 PM
Jin Kazama
Jin Kazama - avatar
+ 2
Use a named mutex with some unique name then check for the enum ERROR_ALREADY_EXISTS I can't remember how the code implementation looks like but I've tried it before on Qt and it works well
21st Feb 2025, 9:47 AM
Badgirl MIRI
Badgirl MIRI - avatar
+ 1
Thanks Bob_Li and Brian Yeah, I too had an idea of title , but it is too initial thought . More than this, is there a way to achieve this as platform independently (both linux and windows ) ?
15th Feb 2025, 1:54 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I was also wondering about that, but it's os specific, it seems.
15th Feb 2025, 2:20 PM
Bob_Li
Bob_Li - avatar
+ 1
To restrict your application to only one instance at a time, you can use various methods depending on the programming language and platform you're using. Here are a couple of common approaches: Using Mutex (Windows) If you are developing a Windows application, you can use a mutex (short for "mutual exclusion") to ensure that only one red https://www.redhumana.com.co instance of your application is running. Here's an example in C#: using System; using System.Threading; class Program { static void Main() { bool createdNew; using (Mutex mutex = new Mutex(true, "MyUniqueApplicationName", out createdNew)) { if (!createdNew) { Console.WriteLine("An instance of the application is already running."); return; } // Your application code here Console.WriteLine("Application is running. Press Enter to exit."); Console.ReadLine(); } } } Using Process Checking (Cross-Platform) Another method is to check for the presence of the application process when the application starts. This approach works on multiple platforms (Windows, Linux, macOS). Here's an example in Python: import psutil import sys def check_if_already_running(): for proc in psutil.process_iter(['pid', 'name']): if proc.info['name'] == 'your_application_name.exe' and proc.info['pid'] != os.getpid(): return True return False if check_if_already_running(): print("An instance of the application is already running.") sys.exit() # Your application code here print("Application is running. Press Enter to exit.") input() Using SingleInstance Class (C++) In C++, you can create a class to handle the single instance requirement: #include <iostream> #include <windows.h> class SingleInstance { public: SingleInstance(const char* name) { m_hMutex = CreateMutex(NULL, TRUE, name); m_bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS); } ~SingleInstance() {
24th Feb 2025, 7:07 AM
venom85jojes
0
21st Feb 2025, 10:21 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks venom85jojes Does python one is platform independent? If proc.info is having extension as .exe for name, will linux also has .exe extension? Regarding last method, singleton class is limited to one application. Right? Or is it singleton across the os and user level ?
24th Feb 2025, 12:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar