Hello everyone! I need to allow only one instance of my application to run.
I use this way for Windows:
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?