App already running

I am writing a desktop app for Windows and need to check if it is already running but cannot see how to do this. Do I need to use the app.appactive as I can’t find any documentation on this in the help?

Look in Example Projects > Platform Specific > Windows > MutexExample
its for exactly this situation

Thanks Norman, I had looked at the examples but didn’t know what a Mutex is which is why I missed it. What does the app.appactive do as it doesn’t appear to be documented?

“App” is a global function in the runtime that returns the reference to the singleton that is your app subclass

http://documentation.xojo.com/index.php/Application.Activate

Ok but if you type the following:

if app.appactive

you will see that there is a boolean value returned but what is the app.appactive returning true/false for as it is not documented anywhere on in the forum anywhere?

not here there isn’t

It’s not here either. Which version is your Xojo install?

and what plugins ?

Ok sorry, it must be coming from a plugin. I am only using MBS Complete and MBS SQL so it must be something to do with one of those. Thanks for the quick response and the mutex solution.

I prefer an approach like this one rather than using a Mutex. It just parses the Wndows tasklist to see if the app’s name is already there.

// Alternatively you can do the same thing without using a Mutex AppName = Left(App.ExecutableFile.Name, len(App.ExecutableFile.Name) - 4) // strip off the .exe extension Dim s As shell = New Shell s.Execute("tasklist /fo CSV") Dim theString As String = s.Result Dim theStringArray() As String theStringArray = Split(theString, AppName) If theStringArray.Ubound>1 Then // There's more than one instance in the list of processes that are running Call MsgBox ("You cannot have more than one instance of this " + _ AppName + " program running at any given time.", 16, "Access Warning...") Quit End If

The problem I’ve had with Mutex in the past is if the app crashes (which of course it shouldn’t do) the Mutex may not get cleaned up properly. (BTW, thanks to whomever it was who originally wrote this code. It wasn’t me but I use it in most of my applications.)

but wont work in all cases either
copy it 3 times
rename each copy
run each copy
they’ll all launch and IF your app assumes exclusivity for some resource you’ll have problems

[quote=292684:@Norman Palardy]but wont work in all cases either
copy it 3 times
rename each copy
run each copy
they’ll all launch and IF your app assumes exclusivity for some resource you’ll have problems[/quote]
True, but I doubt there is any program out there that can’t intentionally be broken.

Here is the code I wrote to check if an application (usually not itself) is running:

[code]Public Function isApplicationRunningWAD(appName As String) as Boolean
Dim tempShell As New Shell
Dim tempString As String

if appName = “” then Return False

tempShell.TimeOut = 30000

#if TargetWin32 then
if right(appName, 4) <> “.exe” then appName = appName + “.exe”
tempString = "tasklist /FI ""IMAGENAME eq " + appName + “”
tempShell.Execute(tempString)
Return InStr(tempShell.Result, appName) > 0

#elseif TargetLinux or TargetMacOS then
if TargetLinux then
tempString = “if pgrep --exact “”” + left(appName, 15) + “”" > /dev/null" + EndOfLine 'Linux and MacOS only display first 15 characters!
elseif TargetMacOS then
tempString = “if pgrep -l -x “”” + left(appName, 15) + “”" > /dev/null" + EndOfLine 'Linux and MacOS only display first 15 characters!
end if
tempString = tempString + “then”+ EndOfLine
tempString = tempString + “echo ““Running””” + EndOfLine
tempString = tempString + “else”+ EndOfLine
tempString = tempString + “echo ““Stopped””” + EndOfLine
tempString = tempString + “fi”+ EndOfLine
tempShell.Execute(tempString)

Return InStr(tempShell.Result, "Running") > 0

#endif

Return False 'never gets here

End Function
[/code]