ExitApp when the user closes the progress bar

Get help with using AutoHotkey and its commands and hotkeys
Flipscript
Posts: 13
Joined: 18 Apr 2017, 10:38

ExitApp when the user closes the progress bar

28 Apr 2019, 05:13

I use a progress bar for my script with the option „M2“ so the progress bar window has a set of the 3 typical buttons, that is minimize, maximize and close, in the title bar.

When the user presses the close button of the progress title bar the script shall terminate (= ExitApp).

What I tried so far was using a loop with the IfWinNotExist command. This way the script terminates when the progress bar is closed but then the rest of the script is not executed because of the loop :facepalm:.

So apart from my approach, do you guys have an idea how I can do this with autohotkey?
wolf_II
Posts: 2618
Joined: 08 Feb 2015, 20:55

Re: ExitApp when the user closes the progress bar

28 Apr 2019, 05:28

Try this:

Code: Select all

Progress, m2 w200, My SubText, My MainText, My Title
WinWaitClose, ahk_class AutoHotkey2
exitApp
I hope that helps.
Flipscript
Posts: 13
Joined: 18 Apr 2017, 10:38

Re: ExitApp when the user closes the progress bar

29 Apr 2019, 17:55

First of all, thank you for your help Wolf!

Your code works insofar that the script terminates when you press the close button of the progress bar.

Unfortunately, the script stops at the line where the WinWaitClose-Command takes in place. But the script shall work in that way that it is executed ordinary. And only if the close button is pressed, the script shall prematurely terminate.

Do you or another user have some further ideas to accomplish this?
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: ExitApp when the user closes the progress bar

29 Apr 2019, 20:40

You could simply add checks for the progress bar window at various points, like so:

Code: Select all

Progress, w200 R0-100 M2,,, Progress Bar

If(!WinExist("Progress Bar"))
    ExitApp

Sleep, 500 ; Replace this line with one or more operations (ex. file copying)
Progress, 25

If(!WinExist("Progress Bar"))
    ExitApp

Sleep, 500 ; Replace this line with one or more operations (ex. file copying)
Progress, 50

If(!WinExist("Progress Bar"))
    ExitApp

Sleep, 500 ; Replace this line with one or more operations (ex. file copying)
Progress, 75

If(!WinExist("Progress Bar"))
    ExitApp

Sleep, 500 ; Replace this line with one or more operations (ex. file copying)
Progress, 100

Sleep, 1000 ; Pause so that the user can see that the bar reached 100%
Progress, Off
A_AhkUser
Posts: 1073
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: ExitApp when the user closes the progress bar

30 Apr 2019, 01:47

Here's another solution, admittedly overkill - if you're committed to using the progress command. Actually, why not using a GUI + GuiClose label instead instead?

Code: Select all

#NoEnv
#SingleInstance force
#Warn

Progress, Hide m2 w200, My SubText, My MainText, My Title
DetectHiddenWindows, On
WinWait, ahk_class AutoHotkey2
OnMessage(0x10, Func("WM_CLOSE").bind(WinExist()))
DetectHiddenWindows, Off
Progress, Show
Loop 4 {
	Progress, % 25 * a_index
Sleep, 1000
}
Progress, Off
OnMessage(0x10, "")
return

WM_CLOSE(hProgress, wParam, lParam, msg, hwnd) {
Critical
if (hwnd = hProgress)
	SetTimer, ExitApp, -10
return 0
}
ExitApp:
ExitApp
Hope this helps.
just me
Posts: 6290
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: ExitApp when the user closes the progress bar

30 Apr 2019, 03:59

... and another one using a timer:

Code: Select all

#NoEnv
#SingleInstance, Force
#Warn

ScriptPID := DllCall("GetCurrentProcessId", "UInt")

Progress, m2 w200, My SubText, My MainText, My Title
WinWait, My Title ahk_class AutoHotkey2 ahk_pid %ScriptPID%
ProgressID := WinExist()
SetTimer, CheckProgress, 10
Loop 4 {
   Progress, % 25 * a_index
   Sleep, 1000
}
SetTimer, CheckProgress, Off
Progress, Off

ExitApp

CheckProgress:
If !WinExist("ahk_id " . ProgressID)
   ExitApp
Return
Flipscript
Posts: 13
Joined: 18 Apr 2017, 10:38

Re: ExitApp when the user closes the progress bar

15 May 2019, 18:45

Alright, I have tested your codes, they all do work :clap: . Sorry for my late response, i simply had no time to continue with my code and in addition it took me some time to check out and understand your codes.

Osprey:
Your simple checks are the simple solution, works fine! But in the end the solutions posted by the other two users are a bit more efficient because they avoid the sleep command which generally seems to be smarter for a code. If I did not understand the other codes or felt overchallenged, your easy solution would have been my sure choice! So thank you for your help Osprey! :thumbup:

A_AhkUser:
Actually using the GuiClose label was my first idea too since i already have a code with a guiclose label that works fine. Though my AHK-skills are rather limited so I dismissed the idea of using a Gui particularly, as you already mentioned, with regard to the already available ahk-progress-window. Your solution is excellent, the only reason I preferred another code is due to the fact that I could implement it easier in pulover’s macro creator :shifty:. Thank you very much for your help %A_AhkUser%! :thumbup:

just me:
In the end i decided to use your code which works as fine as Ahk_Users other suggested code but yours was easier to implement in pulover’s macro creator. However i had to struggle with your if expression. I often have problems to comprehend expressions :problem: , also the variable ahk_id is new to me :think: . The SetTimer loop was unknown to me up to now but I have the feeling it is a pretty important command. I hope it’s not just me, but thank you very much for your help just me! :thumbup:
A_AhkUser
Posts: 1073
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: ExitApp when the user closes the progress bar

15 May 2019, 20:10

Flipscript wrote:
15 May 2019, 18:45
also the variable ahk_id is new to me :think: . The SetTimer loop was unknown to me up to now but I have the feeling it is a pretty important command.
You are right: SetTimer is actually a pretty important command, here it especially relevant because:
SetTimer -remarks wrote: [Timers] run asynchronously, meaning that they will run at the specified frequency (interval) even when the script is waiting for a window, displaying a dialog, or busy with another task.[...]
source: SetTimer
I added comments to just me's script:

Code: Select all

#NoEnv
#SingleInstance force
#Warn
SetWorkingDir % A_ScriptDir

ScriptPID := DllCall("GetCurrentProcessId", "UInt") ; here we get the script's unique Process ID (PID)

Progress, m2 w200, My SubText, My MainText, My Title
WinWait, My Title ahk_class AutoHotkey2 ahk_pid %ScriptPID%
/*
	when using window command such as 'WinWait', the search may be narrowed by specifying more than one criterion
	here we make sure that the window is a progress ('ahk_class AutoHotkey2') AND that it belongs to the current script (by checking against its own PID)
	(see also: https://autohotkey.com/docs/misc/WinTitle.htm#multi)
*/
ProgressID := WinExist()
/*
WinExist returns the Unique ID (HWND) of the first matching window, ID which can be used as WinTitle parameter to identify a window instead of its title
if all four window parameters of 'WinExist' are omitted the "last found" window is used by default; yet, if a matching window comes into existence, 'WinWait'
updates the Last Found Window (see also: https://autohotkey.com/docs/misc/WinTitle.htm#LastFoundWindow and https://autohotkey.com/docs/commands/WinWait.htm#Remarks)
*/
SetTimer, CheckProgress, 10
/*
	here settimer is used because timers run asynchronously, meaning that they will run at the specified frequency (interval) even when the script is waiting for a window, displaying
	a dialog, or busy with another task (see also: https://autohotkey.com/docs/commands/SetTimer.htm#Remarks)
*/
Loop 4 {
   Progress, % 25 * a_index
   Sleep, 1000
}
SetTimer, CheckProgress, Off
Progress, Off

ExitApp

CheckProgress:
If !WinExist("ahk_id " . ProgressID) ; https://autohotkey.com/docs/misc/WinTitle.htm#ahk_id
   ExitApp
Return
cheers
Flipscript
Posts: 13
Joined: 18 Apr 2017, 10:38

Re: ExitApp when the user closes the progress bar

20 May 2019, 08:38

Thanks again A_AhkUser for your help with all your explanations!

And like I already said, your code was perfect too and I didn’t use it simply because I had problems to implement it into Pulover’s Macro Creator. Which on a side note is, alongside with the apparent lack of further development, one of the reasons why I should no longer use PMC once I’m more experienced in dealing with Autohotkey.

If you’re working with easy commands like Run, FileCopy or setting simple variables then a code with unknown functions like DllCall() :geek: , all the use of ID’s or the new concatenate expression go over your head :crazy: . But I could comprehend your comments, so thank you :thumbup:

Return to “Ask For Help”

Who is online

Users browsing this forum: DMDComposer, flyingDman, swagfag and 208 guests