How to prevent running multiple instances?

Discuss any general programming issues here
sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

How to prevent running multiple instances?

Post by sashapont » Sun Mar 11, 2018 5:18 pm

Are there any multyplatform solution n to prevent running multiple instances of app?

User avatar
airsoftsoftwair
Posts: 2572
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany
Contact:

Re: How to prevent running multiple instances?

Post by airsoftsoftwair » Sun Mar 11, 2018 11:00 pm

Call CreatePort() to create a message port. If it fails, the port is already there and the app is already running.

sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

Re: How to prevent running multiple instances?

Post by sashapont » Mon Mar 12, 2018 7:38 pm

Can you give any example?

I try to copy example from doc

Code: Select all

Function p_EventFunc(msg)
  Switch msg.action
  Case "OnUserMessage"
    Switch msg.command
    Case "EXIT"
      DebugPrint("Exit received! Quitting now.")
      End
    Default
      Local t = SplitStr(msg.args, "\0")
      DebugPrint(msg.command, "called with", msg.argc, "argument(s)")
      For Local k = 1 To msg.argc
         DebugPrint("Argument", k .. ":", t[k - 1])
      Next
    EndSwitch
  EndSwitch
EndFunction
CreatePort("MY_COOL_PORT_123")
InstallEventHandler({OnUserMessage = p_EventFunc})
Repeat
  WaitEvent
Forever
And have error
Could not create message port!

PEB
Posts: 176
Joined: Sun Feb 21, 2010 2:28 am

Re: How to prevent running multiple instances?

Post by PEB » Tue Mar 13, 2018 8:02 am

CreatePort() will fail if your program is using RapaGUI.

viewtopic.php?f=4&t=1844

sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

Re: How to prevent running multiple instances?

Post by sashapont » Tue Mar 13, 2018 5:35 pm

no it is not use it&
I use only sql and zip plugins

PEB
Posts: 176
Joined: Sun Feb 21, 2010 2:28 am

Re: How to prevent running multiple instances?

Post by PEB » Wed Mar 14, 2018 9:29 pm

So just put something like this toward the top of your code (before you open the display):

Code: Select all

Local var=?CreatePort("MY_PORT")
If var<>0 Then End()

sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

Re: How to prevent running multiple instances?

Post by sashapont » Fri Mar 16, 2018 10:49 am

Is not work.
App close before starting :(

Debugprint("My var"..var)
out 1256

Is it same number for all os?

User avatar
airsoftsoftwair
Posts: 2572
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany
Contact:

Re: How to prevent running multiple instances?

Post by airsoftsoftwair » Sat Mar 17, 2018 12:41 am

Are you on Mac OS or Linux? CreatePort() currently only works on Amiga and Windows.

sashapont
Posts: 86
Joined: Thu Aug 03, 2017 2:49 pm

Re: How to prevent running multiple instances?

Post by sashapont » Sat Mar 17, 2018 10:08 am

on Mac :(
Are there solution for this os?

User avatar
airsoftsoftwair
Posts: 2572
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany
Contact:

Re: How to prevent running multiple instances?

Post by airsoftsoftwair » Sat Mar 17, 2018 4:54 pm

Maybe set the "LSMultipleInstancesProhibited" switch to FALSE in the Info.plist of the app bundle?

Post Reply