How can I do app timeout itself?
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
How can I do app timeout itself?
Hi
I write small app that call/run some other cmd, and waiting output of those cmd, with RunProgram() with options #PB_Program_Wait|#PB_Program_Hide.
Sometime those cmd hangup, my small app can't exit.
I want to ask: How can I do app timeout itself? Example: If after 5 mins my small app still running, itself will kill those cmd (hangup)
Thanks.
I write small app that call/run some other cmd, and waiting output of those cmd, with RunProgram() with options #PB_Program_Wait|#PB_Program_Hide.
Sometime those cmd hangup, my small app can't exit.
I want to ask: How can I do app timeout itself? Example: If after 5 mins my small app still running, itself will kill those cmd (hangup)
Thanks.
- RSBasic
- Moderator
- Posts: 1218
- Joined: Thu Dec 31, 2009 11:05 pm
- Location: Gernsbach (Germany)
- Contact:
Re: How can I do app timeout itself?
Is your program started via CMD a window? If so, you can send SendMessage_()/PostMessage_() to tell your application that the application is exiting.
If necessary, you can quit with KillThread. (not recommended)
If necessary, you can quit with KillThread. (not recommended)
Re: How can I do app timeout itself?
I'm not sure I understand the question. Do you want to exit your own app or force quit the ones your app launched?
If it's the former, could you do something like this?
If it's the former, could you do something like this?
Code: Select all
EnableExplicit
Global startTime.i, currentTime.i
startTime.i = ElapsedMilliseconds()
Repeat
currentTime = ElapsedMilliseconds()
If currentTime - startTime = 300000 ; 5 minutes
End
EndIf
ForEver
Re: How can I do app timeout itself?
Code: Select all
Define program, startTime.q, timeoutInMilliseconds.q
timeoutInMilliseconds = 1000*60*5 ; 5 minutes
program = RunProgram("program_path", "", "", #PB_Program_Open)
If program = 0
Debug "Error"
End
EndIf
startTime = ElapsedMilliseconds()
While ProgramRunning(program)
If ElapsedMilliseconds() - startTime > timeoutInMilliseconds
Debug "Timeout reached. Terminate the program."
KillProgram(program)
Break
EndIf
Delay(1000) ; Reduce the frequency of checks
Wend
CloseProgram(program)
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: How can I do app timeout itself?
Thank you guys,
Looks like i will put the function of @wombats in one thread, it will solve my the problem!
Looks like i will put the function of @wombats in one thread, it will solve my the problem!
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: How can I do app timeout itself?
@wombats @RSBasic @Sicro
I come with this code, but it has bug, i can't get return of Runprogram if using #PB_Program_Wait option...
any idea to fix "KillProgram(notepad) ;;[ERROR] The specified 'Program' is null." ?
I come with this code, but it has bug, i can't get return of Runprogram if using #PB_Program_Wait option...
Code: Select all
Global startTime.i, notepad.i
Procedure activityMonitor(*dummy)
Repeat
If ElapsedMilliseconds() - startTime > (5 * 1000) *1 ; 5 seconds
Debug "5 seconds"
KillProgram(notepad) ;;[ERROR] The specified 'Program' is null.
EndIf
Delay(250)
ForEver
EndProcedure
monitoringThread = CreateThread(@activityMonitor(), 0)
startTime.i = ElapsedMilliseconds()
notepad = RunProgram("C:\Windows\System32\notepad.exe","","",#PB_Program_Wait)
; do my other stuffs
- FourthStone
- User
- Posts: 26
- Joined: Mon Dec 11, 2017 8:44 am
- Location: Australia
Re: How can I do app timeout itself?
Try initializing 'notepad' before starting the timer, might be a race condition.
EDIT: Also try it with #PB_Program_Open instead
EDIT: Also try it with #PB_Program_Open instead
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: How can I do app timeout itself?
hmm, Do you know how to do that?FourthStone wrote:Try initializing 'notepad' before starting the timer, might be a race condition.
EDIT: Also try it with #PB_Program_Open instead
Re: How can I do app timeout itself?
On windows u could try it with CreateProcess & WaitForSingleObject.
https://docs.microsoft.com/en-us/window ... ngleobject
Check for:
https://docs.microsoft.com/en-us/window ... ngleobject
Check for:
Code: Select all
WAIT_ABANDONED
Re: How can I do app timeout itself?
Code: Select all
Global startTime.i, notepad.i
Procedure activityMonitor(*dummy)
Repeat
If ElapsedMilliseconds() - startTime > (5 * 1000) *1 ; 5 seconds
Debug "5 seconds"
KillProgram(notepad) ;;[ERROR] The specified 'Program' is null.
Break ; Ends thread if it kills notepad.exe
EndIf
Delay(250)
ForEver
EndProcedure
monitoringThread = CreateThread(@activityMonitor(), 0)
startTime.i = ElapsedMilliseconds()
notepad = RunProgram("C:\Windows\System32\notepad.exe","","",#PB_Program_Open)
; do my other stuffs
Delay(20000) ; Pause to simulate : "do my other stuffs"
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: How can I do app timeout itself?
Hi, the notepad is just example...Bisonte wrote:Code: Select all
Global startTime.i, notepad.i Procedure activityMonitor(*dummy) Repeat If ElapsedMilliseconds() - startTime > (5 * 1000) *1 ; 5 seconds Debug "5 seconds" KillProgram(notepad) ;;[ERROR] The specified 'Program' is null. Break ; Ends thread if it kills notepad.exe EndIf Delay(250) ForEver EndProcedure monitoringThread = CreateThread(@activityMonitor(), 0) startTime.i = ElapsedMilliseconds() notepad = RunProgram("C:\Windows\System32\notepad.exe","","",#PB_Program_Open) ; do my other stuffs Delay(20000) ; Pause to simulate : "do my other stuffs"
in fact, I want the RunProgram wait till the app finish, so I force to use #PB_Program_Wait
I don't know how get the handle for RunProgram if using PB_Program_Wait. I am sure there are some PB's internal structure stored that handle, but I don't know how access that handle?
Re: How can I do app timeout itself?
Code: Select all
Global startTime.i, notepad.i
Procedure activityMonitor(*dummy)
Repeat
If IsProgram(notepad)
If ProgramRunning(notepad)
If ElapsedMilliseconds() - startTime > (5 * 1000) *1 ; 5 seconds
Debug "5 seconds"
KillProgram(notepad) ;;[ERROR] The specified 'Program' is null.
Break ; Ends thread if it kills notepad.exe
EndIf
EndIf
EndIf
Delay(100)
ForEver
EndProcedure
monitoringThread = CreateThread(@activityMonitor(), 0)
startTime.i = ElapsedMilliseconds()
notepad = RunProgram("C:\Windows\System32\notepad.exe","","",#PB_Program_Open)
If notepad
While ProgramRunning(notepad)
Delay(10)
Wend
EndIf
Debug "Finished"
Delay(5000)
Debug "Exit"
Because experts ask other questions ...
-
- Enthusiast
- Posts: 790
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: How can I do app timeout itself?
Hi,infratec wrote:Remove your additional 5 marks for an expert, or I'll never help you again.Code: Select all
Global startTime.i, notepad.i Procedure activityMonitor(*dummy) Repeat If IsProgram(notepad) If ProgramRunning(notepad) If ElapsedMilliseconds() - startTime > (5 * 1000) *1 ; 5 seconds Debug "5 seconds" KillProgram(notepad) ;;[ERROR] The specified 'Program' is null. Break ; Ends thread if it kills notepad.exe EndIf EndIf EndIf Delay(100) ForEver EndProcedure monitoringThread = CreateThread(@activityMonitor(), 0) startTime.i = ElapsedMilliseconds() notepad = RunProgram("C:\Windows\System32\notepad.exe","","",#PB_Program_Open) If notepad While ProgramRunning(notepad) Delay(10) Wend EndIf Debug "Finished" Delay(5000) Debug "Exit"
Because experts ask other questions ...
Does it avoid race condition ?