hasenwerk

How do I run a function only up to 5 minutes?

9 posts in this topic

Long time reader, first time poster!

 

I have a bit of a dilemma that I need group support with.

The script that I have is a “mule” that takes jobs from an external source, and then uses that information to move users on a web interface. The issue is sometimes the process of moving of a user on this web page goes off into the weeds for what ever reason, and the script is effectively hung and the current / next user in line to be moved doesn't get moved until someone takes a look to see what happened.

What I would like to do is set a time limit for the task to complete. This task normally takes no more than 2 minutes to complete. I would like to be able to have a condition that says if 5 minutes goes by, stop running the function so that we can close the web browser, report the issue to the HelpDesk, and try the task again.

 

I thought this would be the very simple do until loop however that is not the case. It appears to me that what is between Do and Until needs to complete before the Until condition is examined. So if the function between the do until is stuck it will never make it to the until to see of 5 minutes has passed. I was thinking TimerDiff as the Until condition. Is there any way that I can make a function run for a maximum of 5 minutes and then stop running that function?
 

The only way I can see doing this is to make the function an external .exe, run the program, and poll task manager to see if the program is still running. If it is running for more than 5 minutes, stop the task and close the browser window. Seems like this idea would work, but something tells me I may be missing something obvious, simpler way here . . .

 

This is the code I was originally thinking about . . .

 

            $JobStart                    = TimerInit ()
            $JobCompletedOK              = 0
            Do
                _MyEducationBC_Move_CUPE ()                            ; 
$JobCompletedOK get set to "1" as the end of this function
                If $JobCompletedOK        = 1 then ExitLoop
            Until    TimerDiff ($JobStart) = 5000
            If $JobCompletedOK        = 0 Then
                _Send_Email_To_HelpDesk ()
            EndIf

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$JobStart = TimerInit()
$JobCompletedOK = 0
Do
    If TimerDiff($JobStart) >= 5000 Then; that is 5 seconds by the way
        _FuncToAlertYou()
        ExitLoop
    EndIf
    _MyEducationBC_Move_CUPE() ; $JobCompletedOK get set to "1" as the end of this function
Until $JobCompletedOK
If Not $JobCompletedOK Then
    _Send_Email_To_HelpDesk()
EndIf
;Send("{RIGHT}")

Func _FuncToAlertYou()
    Beep(400, 1000)
EndFunc   ;==>_FuncToAlertYou

 

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks for the reply (and correcting my maths about 5 minutes which would be 5x60x1000 = 30000)... but I don't understand how this would work. 

Right after the DO statement you are asking IF more than X time has happened - only a few milliseconds have passed, so the answer would be "no", End of IF

It would then do the Function where it may or may not get stuck. If it is stuck - that's it go going back to the beginning it's stuck waiting for an event. If successful, the function would finish, the script would finish as it should.

What I need to do is launch the function as a separate thread.

Share this post


Link to post
Share on other sites

As per JohnOne's code, the program will alert you with a beep right after the timerdiff reaches 5000 milli seconds.

 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

The suggested code logic above doesn't work as you never get back to the "has 5 seconds passed" as the script is busy doing the function we are trying to time.

I want something to be able to interrupt the main function if more than X time goes by. So in this example, the function would run for 10 seconds. However, I want to stop it after 5 seconds. I expended your code to show this more effectively and to show that it doesn't work.

Global  $JobStart           = TimerInit()
Global  $JobCompletedOK     = 0

Do
    If TimerDiff($JobStart) >= 5000 Then        ; that is 5 seconds by the way
        _FuncToAlertYou()
        ExitLoop
    EndIf
    _MyEducationBC_Move_CUPE()                   ; $JobCompletedOK get set to "1" as the end of this function
Until $JobCompletedOK

If Not $JobCompletedOK Then
    MsgBox              (0,'Job Didn''t Finish Function','This comes up to alert you of a problem.',0)
EndIf

Func _FuncToAlertYou()
    Beep(400, 1000)
    MsgBox              (0,'Alert Function','This comes up to alert you of a problem.',0)
EndFunc   ;==>_FuncToAlertYou

Func    _MyEducationBC_Move_CUPE    ()
    $Repeat             = 10
    Do
        MsgBox              (0,'Main Function','This will go away after ' & $Repeat & ' seconds',1)
        $Repeat             = $Repeat -1
    Until $Repeat = 1
    $JobCompletedOK     = 1
EndFunc

MsgBox              (0,'The End','Job Completed OK = ' & $JobCompletedOK ,0)

 

Unfortunately, I think the only solution is to compile the function, run it, wait the time I want to wait and see if the process is still running or not.

Share this post


Link to post
Share on other sites

I would highly suggest using the [task manager]. Everything you mentioned is already built in the [task manager]. All you have to do is link the script, set the parameter of when to run, and when to stop. All done!

Share this post


Link to post
Share on other sites

I would highly suggest using the [task manager]. Everything you mentioned is already built in the [task manager]. All you have to do is link the script, set the parameter of when to run, and when to stop. All done!

Task Manager is now the plan - each of these functions I wrote obtains the user information from a .txt file that is put into an 'in box' folder. So it is going to be a lot easier to deal with threads as opposed to functions.

Thanks all for the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now