i have more win32 customer than lnx.
ideas to check if program is already runing ?
ideas to check if program is already runing ?
multiplatform please 
i have more win32 customer than lnx.
i have more win32 customer than lnx.
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
Re: ideas to check if program is already runing ?
I think you must do a specific code for each platform !
So ask for a code for each platform instead ^^
So ask for a code for each platform instead ^^
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed
)
My Website : CeriseCode (Warning : perpetual changes & not completed
Re: ideas to check if program is already runing ?
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 ?
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:
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")- Didelphodon
- 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 ?
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.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")
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.
Re: ideas to check if program is already runing ?
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!
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 ?
I was thinking about something like
But I'm not able to test if it works.
On OS X I can't open a PureBasic created app multiple times.
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()On OS X I can't open a PureBasic created app multiple times.
Re: ideas to check if program is already runing ?
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)
Re: ideas to check if program is already runing ?
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

- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: ideas to check if program is already runing ?
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.kenmo wrote:You could also create a temporary file that acts as a "lock"
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!
PureBasic: Born in 1998 and still going strong to this very day!
Re: ideas to check if program is already runing ?
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...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.
