How to determine if program is already running

I have written a console app that runs briefly and quits. I want to prevent a user from launching it again if it is already running. I have thought about setting a database flag but want to avoid the possibility of the flag not being cleared thus preventing the program from ever running again. Is there a Xojo function that can determine if another program is running? This needs to be cross platform.

In the App.Open event:

Dim m as mutex
m = new mutex(“YourApplicationIdentifierString”)
if not m.TryEnter then
// Application already running
quit
end

Mutex

Thanks. Its so easy if you know where to look.

Tried this, but it is not working … Do not no why. The App still opens more than 1 time in Windows.

Dim MyMutex as mutex

MyMutex = new mutex(“DoorManMutex”)

If not MyMutex.TryEnter then

If MsgBox("We've detected that this application is already running on your computer"+EndOfLine+EndOfLine+_
  "If this is not the case,  a reboot may clear up this problem",64, "Error Encountered") = 0 then
End If

Quit(1)

else

App.UseGDIPlus =True

CriarDB()

End If

A few additional comments: I am in the desktop environment. Changed quit(1) to quit already. But can not make it work. I still can open more that on instance of the App in Windows…

You should retain the mutex object instead of declaring it locally.

Just declare it as an App property and then skip the

Dim MyMutex as Mutex

Thank you. Solved.

Now I see the message box and therefore the mutex is working. The issue is that after the mutex is called, I can not quit the original instance of the App anymore with the Quit menu.

I know the issue is coming from App CancelClose from the Thread Kill below…

I am using the CancelClose to clean the App before it closes, because I have it running after a regular Quit.

if db.Connect then
db.Close
end if

wHome.Thread1.Kill

Return False

//fim

I took out the CancelClose and it is working fine, but I am not sure why sometimes I quit the App and it keeps running in the background. That’s why I started with the CancelClose clean up in the first place.

Why are you connecting to the database to close it?

If Db.Connect

Causes your app to connect to the database, and db.close only runs if your app was able to connect again. I have a feeling this might be where your problem lies.

Greg, I took out the DB.close yesterday and looks like the problem was coming from the thread. Today I could manage to solve what I needed without the thread.

In short, for some reason, the mutex and killing the thread where not allowing the App for a second re-start.

I leave here for the experts …