Jump to content

Creating a timer for active process


GeekGirl
 Share

Recommended Posts

Hi, I am fairly new to AutoIT.  I have been reading through all of the forums and still racking my brain not being able to figure this out.  So I need a timer that starts when notepad is opened.  It can only be opened for 30 seconds.  When it hits 30 seconds I need notepad to close out cleanly (is there a way to have it close out without it asking to save?) and then an error window pop up displaying that they have reached their time limit.  I am able to do the above fine.  The problem is I also need the timer to stop if the user closes out of notepad before the 30 seconds has passed, but I am struggling with how to exit that loop.  I have coded in many different ways, here is the solution I am working with, but can't get it to stop the timer if notepad has been closed prior to the 30 seconds.

NotePad()
Func NotePad()
    WinWaitActive ("[CLASS:Notepad]")
     If WinExists("[CLASS:Notepad]") Then

        ;start Timer
        Dim $i_TimeStamp = TimerInit()
        Dim $i_TimeToCountDownFromSeconds = 30
        Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
        While 1
            If TimerDiff($i_TimeStamp) >= 1000 Then
                ToolTip($i_TimeToCountDownFromSeconds)
                $i_TimeStamp = TimerInit()
                IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
                $i_TimeToCountDownFromSeconds -= 1
    EndIf
    sleep(10)
WEnd
Msgbox(0,"Done!","Done counting down from " & $i_OriginalSeconds  & ".")


                ;WinClose("[CLASS:Notepad]", "")

            Else
                MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Window does not exist")
    EndIf
    EndFunc  ;==>NotePad

Link to comment
Share on other sites

Hi, I am fairly new to AutoIT.  I have been reading through all of the forums and still racking my brain not being able to figure this out.  So I need a timer that starts when the user opens notepad.  They only have 30 seconds to complete their process.  If they pass the 30 seconds notepad needs to close out cleanly (is there a way to have it close out without it asking to save?) and then an error window pop up displaying that they have exceeded the time limit.  I am able to do the above fine.  The problem is I also need the timer to stop if the user closes out of notepad before the 30 seconds has passed, but I am struggling with how to exit that loop.  I have coded in many different ways, here is the solution I am working with, but can't get it to stop the timer if notepad has been closed prior to the 30 seconds.

NotePad()
Func NotePad()
    WinWaitActive ("[CLASS:Notepad]")
     If WinExists("[CLASS:Notepad]") Then

        ;start Timer
        Dim $i_TimeStamp = TimerInit()
        Dim $i_TimeToCountDownFromSeconds = 30
        Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
        While 1
            If TimerDiff($i_TimeStamp) >= 1000 Then
                ToolTip($i_TimeToCountDownFromSeconds)
                $i_TimeStamp = TimerInit()
                IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
                $i_TimeToCountDownFromSeconds -= 1
    EndIf
    sleep(10)
WEnd
Msgbox(0,"Done!","Done counting down from " & $i_OriginalSeconds  & ".")
                ;WinClose("[CLASS:Notepad]", "")
            Else
                MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Window does not exist")
    EndIf
    EndFunc  ;==>NotePad

 

Link to comment
Share on other sites

I am actually going to take that completely out.  That was just for testing purposes.  I just need notepad to to completely close out after the 30 seconds and have that message box titled error say they exceeded the time.  I am getting that far, but what happens is if they do get out of notepad before the 30 seconds,,that timer keeps going.  So it can't move onto the next process.

 

Link to comment
Share on other sites

OMG, I could hug you.  That worked!  Now where do I put the WinClose("[CLASS:Notepad]", "") if they do have notepad still opened after the the 30 seconds have passed?

NotePad()
Func NotePad()
    WinWaitActive ("[CLASS:Notepad]")
     If WinExists("[CLASS:Notepad]") Then

        ;start Timer
        Dim $i_TimeStamp = TimerInit()
        Dim $i_TimeToCountDownFromSeconds = 30
        Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
        While WinExists("[CLASS:Notepad]")
            If TimerDiff($i_TimeStamp) >= 1000 Then
                ToolTip($i_TimeToCountDownFromSeconds)
                $i_TimeStamp = TimerInit()
                IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
                $i_TimeToCountDownFromSeconds -= 1
    EndIf
    sleep(10)
WEnd
Msgbox(0,"Done!","Done counting down from " & $i_OriginalSeconds  & ".") ;testing purposes only

;WinClose("[CLASS:Notepad]", "")
;MsgBox(0,"Error", "Time Out")
            Else
                MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Window does not exist")
    EndIf
    EndFunc  ;==>NotePad

 

Link to comment
Share on other sites

43 minutes ago, GeekGirl said:

The problem is I also need the timer to stop if the user closes out of notepad before the 30 seconds has passed, but I am struggling with how to exit that loop.

https://en.wikipedia.org/wiki/Logical_conjunction

$hTimer = TimerInit()

While BitAND(WinExists("[CLASS:Notepad]"), TimerDiff($hTimer) < 30000)
    ;This loop will exit after 30 sec or if you close notepad window earlier
    Sleep(10)
WEnd

 

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>

NotePad()

Func NotePad()
    If Not WinExists("[CLASS:Notepad]") Then
        Run("Notepad")
    EndIf

    Local $Npad = WinWaitActive("[CLASS:Notepad]")

    ConsoleWrite("Start NotePad timer" & @CRLF)
    ;start Timer
    Local $i_TimeStamp = TimerInit()
    Local $i_TimeToCountDownFromSeconds = 30
    Local $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
    While WinExists($Npad)
        If TimerDiff($i_TimeStamp) >= 1000 Then
            ToolTip($i_TimeToCountDownFromSeconds)
            $i_TimeStamp = TimerInit()
            If $i_TimeToCountDownFromSeconds = 0 Then ExitLoop
            $i_TimeToCountDownFromSeconds -= 1
        EndIf
        Sleep(10)
    WEnd

    ConsoleWrite("NotePad exit" & @CRLF)

    If WinExists($Npad) Then
        WinClose($Npad, "")
        If WinExists("[TITLE:Notepad; CLASS:#32770]", "&Save") Then
            Send("!n")
        EndIf
    EndIf

EndFunc   ;==>NotePad

 

I know that I know nothing

Link to comment
Share on other sites

Hi @GeekGirl,

this is a different approach which seems to be more easy in my opinion:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

_WaitForNotepad()
_WaitThirtySeconds()
_CloseNotepad()

Func _WaitForNotepad()
    Local Const $sNotepad = '[CLASS:Notepad]'

    While Not WinExists($sNotepad)
        Sleep(100)
    WEnd

    Return
EndFunc

Func _WaitThirtySeconds()
    Local Const $hTimer = TimerInit()
    Local Const $iTimeoutInSeconds = 30

    While TimerDiff($hTimer) < ($iTimeoutInSeconds * 1000)
        Sleep(500)
    WEnd
EndFunc

Func _CloseNotepad()
    Local Const $sNotepad        = '[CLASS:Notepad]'
    Local Const $sNotepadProcess = 'notepad.exe'

    If Not WinExists($sNotepad) Then
        Return
    EndIf

    ProcessClose($sNotepadProcess)
    MsgBox(48, 'Demo', 'You reached the time limit.')
EndFunc

At the end it does not matter which variant you use, but I hope this helps you to understand another approach 🤞 .

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

Here's my modification of your example:
 

Run('Notepad.exe')
Sleep(1000)

NotePad("[CLASS:Notepad]")

Func NotePad($sTitle = '[CLASS:Notepad]')
    Local $hWnd = WinGetHandle($sTitle)
    ConsoleWrite('$hWnd: ' & $hWnd & ', ' & VarGetType($hWnd) & @CRLF)

    If WinExists($hWnd) = 1 Then
        WinActivate($hWnd)
        If WinWaitActive($hWnd, 3) = 0 Then
            MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", 'Window exists but failed to activate')
            Return SetError(1, 0, False)
        EndIf


        ;start Timer
        Local $h_TimeStamp = TimerInit(), $h_TooltipTimestamp = TimerInit()
        Local $i_TimeToCountDownFromSeconds = 30
;~      Local $i_OriginalSeconds = $i_TimeToCountDownFromSeconds

        While TimerDiff($h_TimeStamp) <= $i_TimeToCountDownFromSeconds * 1000
            If WinExists($hWnd) = 0 Then
                MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", _
                        "Window closed early, " & $i_TimeToCountDownFromSeconds - Round(TimerDiff($h_TooltipTimestamp), 0) & 's remaining')
                Return ; Not sure if you want to return true or false in this condition
            EndIf

            If TimerDiff($i_TimeToCountDownFromSeconds) >= 1000 Then
                ToolTip('Time remaining: ' & $i_TimeToCountDownFromSeconds - Round(TimerDiff($h_TimeStamp) / 1000, 0) & 's')
                $h_TooltipTimestamp = TimerInit()
;~              If $i_TimeToCountDownFromSeconds = 0 Then ExitLoop
;~              $i_TimeToCountDownFromSeconds -= 1
            EndIf

            Sleep(10)
        WEnd

        MsgBox(0, "Done!", "Done counting down from " & $i_TimeToCountDownFromSeconds & ".")

        ; Close the window if it's still open
        If WinExists($hWnd) Then
            SetExtended(1) ; Set @extended to note that the window was still open at the end of the timer
            Local $hCloseTimer = TimerInit()
            While WinExists($hWnd) And TimerDiff($hCloseTimer) <= 5
                WinClose($hWnd, "") ; Try to close the window gracefully
            WEnd
            WinKill($hWnd) ; Force the window to close, process close would also work if you got the ProcessID
        EndIf

        Return SetError(0, 0, True) ; Return success
    Else
        MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Window does not exist")
    EndIf

    Return SetError(2, 0, False) ; Return failure
EndFunc   ;==>NotePad

 

We ought not to misbehave, but we should look as though we could.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...