How can I do app timeout itself?

Just starting out? Need help? Post your questions and find answers here.
sec
Enthusiast
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?

Post by sec »

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.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How can I do app timeout itself?

Post by RSBasic »

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)
Image
Image
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: How can I do app timeout itself?

Post by wombats »

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?

Code: Select all

EnableExplicit

Global startTime.i, currentTime.i

startTime.i = ElapsedMilliseconds()

Repeat
  
  currentTime = ElapsedMilliseconds()
  
  If currentTime - startTime = 300000 ; 5 minutes
    End
  EndIf
  
ForEver
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: How can I do app timeout itself?

Post by Sicro »

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)
Image
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
sec
Enthusiast
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?

Post by sec »

Thank you guys,

Looks like i will put the function of @wombats in one thread, it will solve my the problem!
sec
Enthusiast
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?

Post by sec »

@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...

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 
any idea to fix "KillProgram(notepad) ;;[ERROR] The specified 'Program' is null." ?
User avatar
FourthStone
User
User
Posts: 26
Joined: Mon Dec 11, 2017 8:44 am
Location: Australia

Re: How can I do app timeout itself?

Post by FourthStone »

Try initializing 'notepad' before starting the timer, might be a race condition.


EDIT: Also try it with #PB_Program_Open instead
sec
Enthusiast
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?

Post by sec »

FourthStone wrote:Try initializing 'notepad' before starting the timer, might be a race condition.


EDIT: Also try it with #PB_Program_Open instead
hmm, Do you know how to do that?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How can I do app timeout itself?

Post by Mijikai »

On windows u could try it with CreateProcess & WaitForSingleObject.
https://docs.microsoft.com/en-us/window ... ngleobject

Check for:

Code: Select all

WAIT_ABANDONED
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: How can I do app timeout itself?

Post by Bisonte »

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"
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
sec
Enthusiast
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?

Post by sec »

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"
Hi, the notepad is just example...
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?
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How can I do app timeout itself?

Post by infratec »

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"
Remove your additional 5 marks for an expert, or I'll never help you again.
Because experts ask other questions ... :wink:
sec
Enthusiast
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?

Post by sec »

infratec wrote:

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"
Remove your additional 5 marks for an expert, or I'll never help you again.
Because experts ask other questions ... :wink:
Hi,
Does it avoid race condition ?
Post Reply