bilbo221 0 Posted September 19, 2020 We just have a simple Windows desktop app -- of which we'd like to prevent multiple instances from running. e.g. like this corneliusconcepts.tech/preventing-multiple-application-instances-delphi-tmutex The problem is, some other apps often prevent our app from creating a mutex entry. Consequently, as the user clicks on the shortcut icon, instead of the initial instance being brought to the foreground, multiple exe files are created. And the task manager just shows multiple instances for each click of the shortcut icon. e.g. MyApp.exe MyApp.exe MyApp.exe MyApp.exe Anyone know a better way to prevent multiple instances in Delphi? Maybe via the second suggestion here https://stackoverflow.com/questions/646480 Thanks! Share this post Link to post
FPiette 382 Posted September 19, 2020 11 minutes ago, bilbo221 said: The problem is, some other apps often prevent our app from creating a mutex entry. Could you elaborate on this subject? You select the mutex name. Choose something unique for your application. I my memory serve me well, there is a single instance component in either JCL or JVCL. Share this post Link to post
Uwe Raabe 2057 Posted September 19, 2020 If a mutex is not working you are probably doing something wrong. 6 Share this post Link to post
timfrost 78 Posted September 19, 2020 I have tried several components over many years, and the only one that has proved simple and reliable under all circumstances is JclAppinst.pas in the JCL. Many options if you need them but for simply checking that there there is only one instance you need only two lines of code added to your DPR: one supplying a GUID and then a call to JclAppInstances.CheckSingleInstance. Share this post Link to post
Vandrovnik 214 Posted September 19, 2020 CreateMutex(nil, false, 'YourNameOfTheMutex'); Test return value. https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createmutexa This does not work? Share this post Link to post
timfrost 78 Posted September 19, 2020 The very simple Mutex may not work if multiple instances of your application may be running in different accounts, with different credentials, or as a service. Share this post Link to post
limelect 48 Posted September 19, 2020 (edited) IsFirstInstance in OgFirst in tponguard librery for free does what you need. the above is old. The new onguard dose it differently but you can find here too Look for first instance https://documentation.help/TurboPack-OnGuard-FMX/documentation.pdf Put this function in the DPR Edited September 19, 2020 by limelect Share this post Link to post
David Heffernan 2345 Posted September 20, 2020 Nobody can diagnose the problem without seeing your code. Once you show us a minimal complete reproduction then it will be straightforward to explain the behaviour of your program. 2 Share this post Link to post
Fr0sT.Brutal 900 Posted September 21, 2020 I use Events for this purpose though it doesn't really matter. What really matters is namespace The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space Share this post Link to post
Remy Lebeau 1393 Posted September 21, 2020 (edited) On 9/19/2020 at 1:10 PM, timfrost said: The very simple Mutex may not work if multiple instances of your application may be running in different accounts, with different credentials, or as a service. A simple mutex will work fine in those cases if you create the mutex in the 'Global\...' namespace so it exists across account/session boundaries. See Kernel Object Namespaces. Edited September 21, 2020 by Remy Lebeau Share this post Link to post
timfrost 78 Posted September 21, 2020 But lack of SeCreateGlobalPrivilege privilege may cause creation of the Mutex to fail, as was mentioned by the OP. Share this post Link to post
Remy Lebeau 1393 Posted September 22, 2020 18 hours ago, timfrost said: But lack of SeCreateGlobalPrivilege privilege may cause creation of the Mutex to fail No, it won't. SeCreateGlobalPrivilege applies only to file mapping objects, not to mutexes. Anyone can create a mutex in the Global namespace. See https://stackoverflow.com/a/41370046/65863 18 hours ago, timfrost said: as was mentioned by the OP. I see no mention of this anywhere in this discussion before your comment. 2 Share this post Link to post