Jump to content
bilbo221

Best way to prevent multiple instances? Mutex not working

Recommended Posts

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
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

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.

  • Like 1

Share this post


Link to post

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

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.  

  • Like 2

Share this post


Link to post

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
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 by Remy Lebeau

Share this post


Link to post
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.

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×