ideas to check if program is already runing ?
Posted: Mon Aug 01, 2011 11:57 am
multiplatform please 
i have more win32 customer than lnx.
i have more win32 customer than lnx.
http://www.purebasic.com
https://www.purebasic.fr/english/
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)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.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")
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()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)
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"
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.