DeskTop App

Hi
i use this code for now when the app is running how i do Set Focus to app is running or find the instant on app is running

If (App.PrevInstance = True) Then
MsgBox(“The application is already running!”)
Quit
End

I use mutex to test for a previous instance, take a look at this command in the help file which may sssist.

That’s what the op’s code relies on. It’s just copy/pasted.

I think that @Alexis Colon Lugo is asking about how to do something that I’m not sure can be done:

When a second instance of his app starts, he wants it to realize that a first instance is already running, and have the second one quit and bring the first one to the forefront.

There are probably declares for this?

Aaron published an example a long long long time ago
Used an IPC Socket so the new instance could hand off any opened documents to the new instance (say you dragged a document the app could open on to it and got a second copy)

Otherwise if you dont care about that you could use a declares to hunt down the app & bring it forward

I tend not to use a Mutex for this since if the application crashes before the mutex can be released the user will have to do it manually. If all I want to do is make sure that there cannot be multiple instances of the application running simultaneously, I prefer to examine the system’s task list to see if the application’s name is already there. It can usually be easily done with a shell command. Something like this, on Windows…
Dim s As shell = New Shell s.Execute("tasklist /fo CSV") Dim theString As String = s.Result Dim theStringArray() As String theStringArray = Split(theString, App.ExecutableFile.Name) If theStringArray.Ubound>1 Then // Disallow more than one instance of app 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

It can’t be much different on a Mac or Linux.

thanks

[quote=258481:@Dale Arends] It can usually be easily done with a shell command. Something like this, on Windows…
[/quote]

You might increase the default timeout on the shell. I tried this code and first time it timed out - PC probably having a cramp or pondering the meaning of life or something.

I don’t know if this works ok on Windows but just locking a file works on Linux. When the program quits or crashes, the OS automatically releases it.

Something like:
Add a property App.bsLock of type Binarystream.
then:

  Dim f As FolderItem = SpecialFolder.Temporary.Child("SomeUniqueName" + ".lck")
  App.bsLock = BinaryStream.Create(f, True)

This should raise an IOException on the second instance.
Maybe interesting to test on Windows.

If you hold a file open for writing in Windows for the duration of the run, that would definitely cause an ioexception second time round.

Yes. So if you catch the IOException, you know there is an instance running already.
The thing is that Mutexes are not always properly released when the App crashes. A file lock should always be released by the OS when the App is no longer there.

Tell that to the Windows runtime installer I just had a fight with :stuck_out_tongue: