www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Running only one instance of the app

reply Bagomot <bagomot gmail.com> writes:
Hello everyone! I need to allow only one instance of my 
application to run.

I use this way for Windows:
```d
import std.stdio;
import std.utf;

version(Windows) {
	import core.sys.windows.winbase;
	import core.sys.windows.windows;
}

void main(string[] args) {
	string mutexName = "xxxx";

	version(Windows) {
		HANDLE mutex; //global handle mutex
		try {
			mutex=CreateMutex(NULL, true, uuidMutexName.toUTF16z);
			DWORD result;
			result = WaitForSingleObject(mutex, 0);
			if(result != WAIT_OBJECT_0) {
				writeln("The app is already running!");
   				return;
   			}
		} catch(Exception e) {
			writeln(e.msg);
		}

		scope(exit) {
			ReleaseMutex(mutex);
			CloseHandle(mutex);
		}
	}

	//todo

	while(true){}
}
```
This is where the application creates a global mutex with a 
specific name. At startup, the application checks for the 
presence of such a mutex. If it is, then a instance of the 
application has already been launched.

This works for Windows. I need something similar for Linux and 
Macos.

Tell me how, if you know. Maybe the standard library D already 
has the required functionality? Or are there better ways to only 
run one instance of the app?
Dec 02 2021
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/2/21 7:05 AM, Bagomot wrote:

 This works for Windows. I need something similar for Linux and Macos.
 
 Tell me how, if you know. Maybe the standard library D already has the 
 required functionality? Or are there better ways to only run one 
 instance of the app?
Typically this is done using pid files. https://linux.die.net/man/3/pidfile But I don't know if MacOS has a specialized call for it, or if you have to do it yourself. I think you need to lock the file with `flock` to get it to work properly. -Steve
Dec 02 2021
parent Bagomot <bagomot gmail.com> writes:
On Thursday, 2 December 2021 at 13:28:12 UTC, Steven 
Schveighoffer wrote:
 On 12/2/21 7:05 AM, Bagomot wrote:

 This works for Windows. I need something similar for Linux and 
 Macos.
 
 Tell me how, if you know. Maybe the standard library D already 
 has the required functionality? Or are there better ways to 
 only run one instance of the app?
Typically this is done using pid files. https://linux.die.net/man/3/pidfile But I don't know if MacOS has a specialized call for it, or if you have to do it yourself. I think you need to lock the file with `flock` to get it to work properly. -Steve
I can't use flock, because my app uses the file rename to auto-update in Windows. It renames itself to old.app.exe, downloads the new version and runs it, then exits. I haven’t figured out how to do this in other OSs yet.
Dec 02 2021
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 2 December 2021 at 12:05:44 UTC, Bagomot wrote:
 Hello everyone! I need to allow only one instance of my 
 application to run.

 [...]
Aren't you just supposed to look at prevInstance
Dec 04 2021
prev sibling parent AnimusPEXUS <animuspexus protonmail.com> writes:
If I would need to solve this question, I'd used pipe files or 
unix files. Simply create one from main instance and reply some 
`{"status": "running"}` json to it and try reading this pipe in 
app start
Dec 04 2021