Page 1 of 1

[Solved] CreateMutex allowing multiple instances

Posted: Mon Nov 11, 2019 10:06 am
by BarryG
I've got this code near the start of my source:

Code: Select all

Global appmutex=CreateMutex_(0,1,"My_App_Unique_ID")
MessageRequester("RunOnce","Mutex: "+Str(appmutex))
But when I compile it to an exe and run several instances of it, the "appmutex" variable is always non-zero, meaning the mutex was successfully created for each instance (see screenshot).

However, it's not supposed to, and it should return 0 in the MessageRequester() when a match for the mutex name is already found. Same if I use a semaphore.

This used to work before to prevent multiple instances of my app (only run once), but not anymore. Using Windows 10 Pro, with both 32-bit and 64-bit of PureBasic 5.71. Any ideas?

Re: CreateMutex allowing multiple instances

Posted: Mon Nov 11, 2019 11:49 am
by Marc56us
Forgot to test the return error?
GetLastError_()=#ERROR_ALREADY_EXISTS

Code: Select all

; Determine if only one instance is running
; https://rosettacode.org/wiki/Determine_if_only_one_instance_is_running#PureBasic
;
#MyApp="MyLittleApp"
Mutex=CreateMutex_(0,1,#MyApp)
If GetLastError_()=#ERROR_ALREADY_EXISTS
  MessageRequester(#MyApp,"One instance is already started.")
  End
EndIf
 
; Main code executes here
 
ReleaseMutex_(Mutex)
End
Source: https://rosettacode.org/wiki/Determine_ ... #PureBasic
Windows 10 x64 family: OK
(Not tested elsewhere) :wink:

Re: CreateMutex allowing multiple instances

Posted: Mon Nov 11, 2019 12:43 pm
by BarryG
Solved. It was nothing to do with GetLastError_(), which yes, I was also checking but didn't show in my example. Here's how it was solved: my app, when detecting the existing mutex, was sending a #HWND_BROADCAST message to the first instance. However, since it was being done with SendMessage_(), the second instance wasn't returning to quit itself with "End". I simply changed SendMessage_() to PostMessage_() and it now works again.