Jump to content

Exiting a while loop by clicking a button


Recommended Posts

Hi, everybody!

I am writing an mp3 encoding program. During the encoding process, I want to have a cancel button but it seems that using case to detect the button has been pressed or using GUICtrlSetOnEvent do not work because (I think) RunWait(ffmpeg.exe.....) is stopping the script during the encode process and the button click is not recognized. HotKeySet detects a key press at any time and I have that working. I would rather have a button than ask the user to press ESC. Just wanted to see if there are any "simple" answers. I am not so advanced to understand pointers and memory reads.

I can write up some example code if anyone wants, but to test you will need ffmpeg.exe (or maybe I can write a second exe that just takes up 5 seconds or so). 

It's curious, I searched DDG and only saw a couple questions on this topic. I have been working on this for a whole day and I would think more people would have this problem. Maybe it's just me...

Thanks!

Link to comment
Share on other sites

2 hours ago, abberration said:

...  any "simple" answers ...

a possible way

#include <WinAPISys.au3>
#include <WindowsConstants.au3>

GUICreate("Test", 100, 80, 50, 50, -1, $WS_EX_TOPMOST)
Global $hBtn = GUICtrlCreateButton("Kill it", 10, 10, 80, 60)
GUISetState()

; set up a call to the button control function
; it is also called regardless RunWait() pauses the execution of the script
;                   -----------------------------------------
$hTimerProc = DllCallbackRegister('_Check_button', 'none', 'hwnd;uint;uint_ptr;dword')
$iTimerID = _WinAPI_SetTimer(0, 0, 50, DllCallbackGetPtr($hTimerProc))

; run your program with long-term execution (this one is an endless ping example)
RunWait("ping.exe localhost -t")  ; use ffmpeg.exe here instead

MsgBox(0, "Done", "Program killed")

Func _Check_button($hWnd, $iMsg, $iTimerID, $iTime)
    #forceref $hWnd, $iMsg, $iTimerID, $iTime
    If GUIGetMsg() = $hBtn Then
        _WinAPI_KillTimer(0, $iTimerID) ; stop checking the button
        ProcessClose("Ping.exe") ; use ffmpeg.exe here instead
    EndIf
EndFunc   ;==>_Check_button

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Here is another possible way:

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $Form1, $Btn_Start, $Btn_Stop, $p_pid

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 207, 45, 192, 124)
$Btn_Start = GUICtrlCreateButton("Start", 6, 7, 75, 27)
$Btn_Stop = GUICtrlCreateButton("Stop", 95, 7, 93, 27)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetState ($Btn_Stop, $gui_DISABLE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Btn_Start
            GUICtrlSetState ($Btn_Stop, $GUI_ENABLE)
            GUICtrlSetState ($Btn_Start, $GUI_DISABLE)
            
            $p_pid=ShellExecute("cmd.exe","/c dir C:\ /S /oN")
            ConsoleWrite ("PiD= " & $p_pid & @CRLF)
            While ProcessExists($p_pid)
                Sleep (15)
                $nMsg = GUIGetMsg()
                if $nMsg=$Btn_Stop then ProcessClose($p_pid)
            Wend

            GUICtrlSetState ($Btn_Stop, $GUI_DISABLE)
            GUICtrlSetState ($Btn_Start, $GUI_ENABLE)
    EndSwitch
WEnd

 

Some of my script sourcecode

Link to comment
Share on other sites

7 hours ago, abberration said:

Just wanted to see if there are any "simple" answers

The simplest one I can think of is to not use RunWait  :)
I personally always use Run(ffmpeg.exe.....) and a While loop so I can read Stdout (to display a progressbar)
This loop can be interrupted by a simple $stop variable which I set using a button and WM_COMMAND

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