Page 1 of 1

ideas to check if program is already runing ?

Posted: Mon Aug 01, 2011 11:57 am
by sartic
multiplatform please :)
i have more win32 customer than lnx.

Re: ideas to check if program is already runing ?

Posted: Mon Aug 01, 2011 12:21 pm
by graph100
I think you must do a specific code for each platform !
So ask for a code for each platform instead ^^

Re: ideas to check if program is already runing ?

Posted: Mon Aug 01, 2011 12:57 pm
by Foz
Open a networking port to listen on. Only one application at a time can lock a port.

Code: Select all

InitNetwork()

If Not CreateNetworkServer(0, 55555)
  ; can't create - port in use error usually!
  MessageRequester("", "Already running!")
  End
EndIf

OpenWindow(0, 0, 0, 230, 90, "There can only be ONE", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Repeat
  Event = WaitWindowEvent()
  
Until Event = #PB_Event_CloseWindow

; keep the port open for the duration of the program
CloseNetworkServer(0)

Re: ideas to check if program is already runing ?

Posted: Tue Aug 02, 2011 12:19 am
by kenmo
You could also create a temporary file that acts as a "lock". It's primitive, but I have seen programs doing it (including big commercial software) as long as I can remember. On Windows, you could set the file as hidden, too.

Modifying Foz's example:

Code: Select all

If FileSize("MyProg.lock") >= 0
  MessageRequester("", "Already running!")
  End
EndIf

If CreateFile(0, "MyProg.lock")
  CloseFile(0)
EndIf

OpenWindow(0, 0, 0, 230, 90, "There can only be ONE", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

DeleteFile("MyProg.lock")

Re: ideas to check if program is already runing ?

Posted: Tue Aug 02, 2011 10:45 am
by Didelphodon
kenmo wrote:You could also create a temporary file that acts as a "lock". It's primitive, but I have seen programs doing it (including big commercial software) as long as I can remember. On Windows, you could set the file as hidden, too.

Modifying Foz's example:

Code: Select all

If FileSize("MyProg.lock") >= 0
  MessageRequester("", "Already running!")
  End
EndIf

If CreateFile(0, "MyProg.lock")
  CloseFile(0)
EndIf

OpenWindow(0, 0, 0, 230, 90, "There can only be ONE", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

DeleteFile("MyProg.lock")
As long as I know on Linux creating/writing files for such an situation is not sufficient as you'd have to additionally request "exclusive" access which is not the default and probably IMHO also not specified by PB's CreateFile.

So on Linux my preferred solution for this is also to create a network-server. But be aware that you'll have to use high-ports otherwise you would have to run your program with root.

Re: ideas to check if program is already runing ?

Posted: Tue Aug 02, 2011 1:05 pm
by Foz
Using the file system has one other flaw: if you crash out, you will be locked out until you delete the file.

About the ports: note that was why I had the port number being 55555 - perfect for temporary use!

Re: ideas to check if program is already runing ?

Posted: Tue Aug 02, 2011 2:36 pm
by wilbert
I was thinking about something like

Code: Select all

AppName.s = "Open Check"

Procedure ThreadUpdatePrefs(Parameter)
  Repeat
    If CreatePreferences("RunCheck.prefs")
      WritePreferenceLong("check", Date())
      ClosePreferences()
    EndIf
    Delay(19000)
  ForEver
EndProcedure

OpenPreferences("RunCheck.prefs")
If Date() - ReadPreferenceLong("check", 0) < 20000
  MessageRequester("Information", AppName + " is already running !", #PB_MessageRequester_Ok)
Else
  Thread = CreateThread(@ThreadUpdatePrefs(), 0)
  
  ; *** main code ***
  
  If OpenWindow(0, 100, 200, 195, 260, AppName, #PB_Window_SystemMenu)
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
    
  EndIf
  
  ; *** end of main code ***
  
  KillThread(Thread)
  If CreatePreferences("RunCheck.prefs")
    WritePreferenceLong("check", 0)    
  EndIf
EndIf
ClosePreferences()
But I'm not able to test if it works.
On OS X I can't open a PureBasic created app multiple times.

Re: ideas to check if program is already runing ?

Posted: Wed Aug 03, 2011 8:59 am
by sartic
nice idea, i will play width it :)
Foz wrote:Open a networking port to listen on. Only one application at a time can lock a port.

Code: Select all

InitNetwork()

If Not CreateNetworkServer(0, 55555)
  ; can't create - port in use error usually!
  MessageRequester("", "Already running!")
  End
EndIf

OpenWindow(0, 0, 0, 230, 90, "There can only be ONE", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Repeat
  Event = WaitWindowEvent()
  
Until Event = #PB_Event_CloseWindow

; keep the port open for the duration of the program
CloseNetworkServer(0)

Re: ideas to check if program is already runing ?

Posted: Wed Aug 03, 2011 9:04 am
by sartic
Do not like width thread. I made one watchdog (for another problem, not run only once) for linux... and still can not understand why it fails sometime. 99% working, 1% fail.

Re: ideas to check if program is already runing ?

Posted: Wed Aug 03, 2011 2:09 pm
by MachineCode
kenmo wrote:You could also create a temporary file that acts as a "lock"
VERY unprofessional, even for big-name companies! As mentioned, if your app crashes, or gets ended by the user in the Task Manager, or doesn't respond to a logoff/reboot message, then your app WILL NOT START in future, because it thinks it's still running.

But if you insist on using this bad approach, then you'll need to ensure the "lock" file is saved to a writable (and preferably hidden) folder. Don't save it to the Windows "Temp" folder, as many cleaning apps (such as "CCleaner") will delete the files in that folder. And don't save it to the app's folder, because the app might be launched from read-only medium, like a locked SD card or DVD-ROM.

Re: ideas to check if program is already runing ?

Posted: Sat Aug 06, 2011 12:44 am
by kenmo
MachineCode wrote:VERY unprofessional, even for big-name companies! As mentioned, if your app crashes, or gets ended by the user in the Task Manager, or doesn't respond to a logoff/reboot message, then your app WILL NOT START in future, because it thinks it's still running.
Yep, I've had to manually delete a couple of those files before, after crashes. I could list specific software but I won't name names... 8)