ideas to check if program is already runing ?

Linux specific forum
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

ideas to check if program is already runing ?

Post by sartic »

multiplatform please :)
i have more win32 customer than lnx.
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: ideas to check if program is already runing ?

Post by graph100 »

I think you must do a specific code for each platform !
So ask for a code for each platform instead ^^
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: ideas to check if program is already runing ?

Post 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)
User avatar
kenmo
Addict
Addict
Posts: 2051
Joined: Tue Dec 23, 2003 3:54 am

Re: ideas to check if program is already runing ?

Post 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")
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Re: ideas to check if program is already runing ?

Post 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.
Go, tell it on the mountains.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: ideas to check if program is already runing ?

Post 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!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ideas to check if program is already runing ?

Post 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.
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

Re: ideas to check if program is already runing ?

Post 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)
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

Re: ideas to check if program is already runing ?

Post 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.
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: ideas to check if program is already runing ?

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
kenmo
Addict
Addict
Posts: 2051
Joined: Tue Dec 23, 2003 3:54 am

Re: ideas to check if program is already runing ?

Post 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)
Post Reply