How to implement progress bar Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

How to implement progress bar

28 Jan 2018, 10:08

I've created two scripts just so I can create an instant restore point in Windows 10. Functionally they are identical but one wraps WMI and the other wraps PowerShell.They work great... but the only visual indicator that they are working is the tray icon. Once they finish they pop a MsgBox to that effect. What I would like is to add a progress bar. It doesn't need to do anything other than show more clearly that the script (either one) hasn't yet finished.

I found the following which displays a marquee-type progress bar but, despite many attempts, have not been able to incorporate it so it starts, runs whilst the restore point is being created then disappears again. I'm sure I'm doing (or not doing) something stupid but just can't work it out. Any help would be gratefully received, either to incorporate the progress bar or a different/better visual indicator.

Progress Bar:

Code: Select all

; https://autohotkey.com/board/topic/12306-infinite-progress-bar/#entry79882

Gui, Add, Progress, vlvl -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
Gui, Show, , Example
SetTimer, Push, 45
Push:
	GuiControl, , lvl, 1
	Return
GuiClose:
	ExitApp
Create instant restore point using WMI:

Code: Select all

; Prompt to 'Run as Admin', i.e. show UAC dialog
if Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%", , UseErrorLevel ; Requires AutoHotkey v1.0.92.01+
ExitApp
}

; Note: In Win 10 there's a frequency restriction about how often restore points can be created.
; Use the following to remove the restriction.
SetRegView 64 ; Allow registry commands in a 32-bit script to access redirected keys in the 64-bit registry view and vice versa
RegRead, vOS, HKLM\Software\Microsoft\Windows NT\CurrentVersion, ProductName
IfInString, vOS, Windows 10
{
RegWrite, REG_DWORD, HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore, SystemRestorePointCreationFrequency, 0
}
SetRegView default ; Restore normal behaviour for 'Reg' commands

; Ask for the name to be used...
InputBox, vDesc, Restore Point Name, Input a name for your restore point, , 320, 130

objWMIService := ComObjGet("winmgmts:\\.\root\default:Systemrestore")
objService := objWMIservice.CreateRestorePoint( vDesc, 12, 100)
objWMIService ()
objService ()

; Pop message to inform user
MsgBox, 64, Instant Restore Point, A restore point has been created, 2
ExitApp
Create instant restore point using PowerShell:

Code: Select all

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires AutoHotkey v1.0.92.01+
   ExitApp
}

; Note: In Win 10 there's a frequency restriction about how often restore points can be created.
; Use the following to remove the restriction.
SetRegView 64 ; Allow registry commands in a 32-bit script to access redirected keys in the 64-bit registry view and vice versa
RegRead, vOS, HKLM\Software\Microsoft\Windows NT\CurrentVersion, ProductName
IfInString, vOS, Windows 10
{
RegWrite, REG_DWORD, HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore, SystemRestorePointCreationFrequency, 0
}
SetRegView default ; Restore normal behaviour for 'Reg' commands

; Ask for the name to be used...
InputBox, vDesc, Create Instant Restore Point, Description?, , 320, 130
if ErrorLevel
   {
   MsgBox, CANCEL was pressed.
   ExitApp
   }
else
    psScript =
   (
      Checkpoint-Computer -Description '%vDesc%' -RestorePointType "MODIFY_SETTINGS"
   )
; Note the use of single quotes around the 'description' variable.
; This is one of the quirks of wrapping PowerShell cmdlets in AHK

; Use this call if you don't want to see PowerShell output
RunWait PowerShell.exe -Command %psScript% ,, hide

; Use this call if you want to see PowerShell output
;Run powershell.exe -NoExit -Command %psScript%
; Pop message to inform user
MsgBox, 64, Instant Restore Point, A restore point has been created, 2
ExitApp
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: How to implement progress bar  Topic is solved

28 Jan 2018, 11:03

EDIT: I'm glad the modified PS script worked for you. Thanks for letting me know about the results of the WMI one

For the PowerShell variant, this might work (keep/remove/change the GuiClose label which I copied from your post if desired):

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires AutoHotkey v1.0.92.01+
   ExitApp
}

; Note: In Win 10 there's a frequency restriction about how often restore points can be created.
; Use the following to remove the restriction.
SetRegView 64 ; Allow registry commands in a 32-bit script to access redirected keys in the 64-bit registry view and vice versa
RegRead, vOS, HKLM\Software\Microsoft\Windows NT\CurrentVersion, ProductName ; https://autohotkey.com/docs/Variables.htm#OSVersion?
IfInString, vOS, Windows 10
{
RegWrite, REG_DWORD, HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore, SystemRestorePointCreationFrequency, 0
}
SetRegView default ; Restore normal behaviour for 'Reg' commands

; Ask for the name to be used...
InputBox, vDesc, Create Instant Restore Point, Description?, , 320, 130
if ErrorLevel
   {
   MsgBox, CANCEL was pressed.
   ExitApp
   }
else
    psScript =
   (
      Checkpoint-Computer -Description '%vDesc%' -RestorePointType "MODIFY_SETTINGS"
   )
; Note the use of single quotes around the 'description' variable.
; This is one of the quirks of wrapping PowerShell cmdlets in AHK

; https://autohotkey.com/board/topic/12306-infinite-progress-bar/#entry79882
Gui, Add, Progress, HwndpHwnd1 -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
PostMessage, 0x40a, 1, 38, , ahk_id %pHwnd1% ;_wParam-Bool (1 = start , 0 = stop),lParam-Time (ms) between animation updates. ; PBM_SETMARQUEE //autohotkey.com/board/topic/24700-indeterminate-progressbar/?p=160024
Gui, Show, , Example

; Use this call if you want to see PowerShell output
;Run powershell.exe -NoExit -Command %psScript%,,, psPid
; Use this call if you don't want to see PowerShell output
Run PowerShell.exe -Command %psScript% ,, hide, psPid

; pid not zero and handle to process opened
if ((psPid) && (hProc := DllCall("OpenProcess", "UInt", SYNCHRONIZE := 0x00100000, "Int", False, "UInt", psPid, "Ptr"))) {
	MsgWaitForMultipleObjectsEx := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", "MsgWaitForMultipleObjectsEx", "Ptr")
	Loop
		r := DllCall(MsgWaitForMultipleObjectsEx, "UInt", 1, "Ptr*", hProc, "UInt", 0xFFFFFFFF, "UInt", 0x4FF, "UInt", 0x6, "UInt"), Sleep -1
	Until (r == 0 || r == 0xFFFFFFFF) ; stolen from Lexikos: wait on the process to terminate, while allowing messages to be pumped etc. Break when process object is signalled (closed) or failure
	DllCall("CloseHandle", "Ptr", hProc)
	Gui, Destroy

	; Pop message to inform user
	MsgBox, 64, Instant Restore Point, A restore point has been created, 2
} ; else, something went wrong

GuiClose:
	ExitApp
For the WMI one, adding

Code: Select all

; https://autohotkey.com/board/topic/12306-infinite-progress-bar/#entry79882
Gui, Add, Progress, HwndpHwnd1 -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
PostMessage, 0x40a, 1, 38, , ahk_id %pHwnd1% ;_wParam-Bool (1 = start , 0 = stop),lParam-Time (ms) between animation updates. ; PBM_SETMARQUEE //autohotkey.com/board/topic/24700-indeterminate-progressbar/?p=160024
Gui, Show, , Example
right after the InputBox command might work. Or the GUI might block up and the progress bar won't update and/or you can't move the window while CreateRestorePoint is doing its thing. I can't test anything as I don't have System Restore enabled.
Last edited by qwerty12 on 28 Jan 2018, 12:39, edited 1 time in total.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: How to implement progress bar

28 Jan 2018, 11:21

@qwerty12 - The PowerShell amendment works perfectly. :bravo:

As you suspected, the WMI variant locked up until the restore point had been created.

However, no matter. I only needed one to work so thank you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Rohwedder, RussF and 309 guests