Mutex problem

Implementing a mutex to avoid running the same application two times, I found a problem on the suggested implementation in Xojo docs.
The docs says:

Add a property to your app:

mMutex As Mutex

In App.Open event handler, you can attempt to create a Mutex.

[code]mMutex = New Mutex(“MutexExample”)

If Not mMutex.TryEnter Then
MsgBox(“You cannot have more than one copy of this app running!”)
Quit
End If
[/code]

In App.Close, you can release the Mutex:

If mMutex <> Nil Then mMutex.Leave End If

The problem raise on the App.Close event: when the Mutex failed to enter, the mMutex.Leave cause an IllegalLockingException:
ErrorNumber: 3
Message: The thread which Signaled the Mutex must be the thread to Release the Mutex

This happen on OS X, not yet tried on Windows.

Any suggestions, apart of handling and ignoring the exception? It could be a bug?

I don’t know.
How about adding a property bMutexSuccess As Boolean that defaults to True.
Then

mMutex = New Mutex("MutexExample") If Not mMutex.TryEnter Then bMutexSuccess=False MsgBox("You cannot have more than one copy of this app running!") Quit End If

and

If bMutexSuccess=True and mMutex <> Nil Then mMutex.Leave End If

Would that help?
I’m thinking it isn’t able to leave because it was never able to enter

Yes, the exception happens because the mutex was not able to enter, but it should not raise an exception on Leave.
And the exception message smells of bug: "The thread which Signaled the Mutex must be the thread to Release the Mutex”

Anyway I solved putting the Mutex.Leave in a try…end try block

I think that’s a new msg which might be triggered here by mistake.
As far as I can tell they should both have been called from the main thread (app.open and app.close), and therefore you should never get this msg.
You might want to file a feedback report.

Have a look at https://forum.xojo.com/8531-what-is-an-illegallockingexception .

I use this code snippet in all my apps (Windows though, no use for Mac really), but I don’t use the .Leave thing because when the app quits the Mutex goes away automatically. (Specifically, the file in “temp” that represents the Mutex is deleted.)

For what reasons are you doing this explicitly?

I have cases where the file is not deleted and, to be sure I always leave.
Anyway this is the suggested way in the Xojo docs about Mutex class.

mMutex = New Mutex("MutexExample")

If Not mMutex.TryEnter Then
  MsgBox("You cannot have more than one copy of this app running!")
  mMutex = nil
  Quit
End If

Yes Norman, this is another way I tested and it works.
But it would be better to update the docs to avoid people falling again on this problem.

There’s several ways to rewrite this - like I did to add the nil of the property
Or

Add a property to your app:

mMutex As Mutex

In App.Open event handler, you can attempt to create a Mutex.

dim tmp as New Mutex("MutexExample")

If Not tmp.TryEnter Then
  MsgBox("You cannot have more than one copy of this app running!")
  Quit
else
  mMutex = tmp
End If

In App.Close, you can release the Mutex:

If mMutex <> Nil Then mMutex.Leave End If

And I’m sure there are others that would all work

I think you can just let the mutex go nil, I’ve never called Mutex.Leave. Documentation be damned! :slight_smile: