Jump to content

Global Pause, and general pause functions.


JohnOne
 Share

Recommended Posts

I've made a makeshift universal pause function hack, which works by an external script controlling AutoIt's tray pause capability.
 
Here's how it works,,,
 
You Include the GlobalPause.au3 in your script, when you do that and run your script, it writes Pause.au3 to your script folder. Pause.au3 is the binary string which you see in GlobalPause.au3, it's just much neater to write it this way, The actual code of Pause.au3 is below.
 
Your script (shown below as TestingPause.au3) needs everything you see in it except the Sleep and ConsoleWrite which is just there for demonstration purposes.
 
It's pretty self explanatory really, you hit pause key and it should  pause your script immediately, you press it again and it will resume it as though you hit the pause button in the system tray. That is all it is, automating that button and a bit of logic, the reason is just convenience and I've seen many people wanting a boil in the bag pause function.
 
Most of the automation code comes from M32 / rover in >this post, or one similar to it (cannot quite remember, I found a few), with a few adjustments.
 
Here is the setback, your script's system tray icon must be visible in the tray and not hidden, I simply cannot find a way to automate the hidden ones. You can make it stay there by using the customize icon link in the hidden tray.
 
The Code

 The Include: GlobalPause.au3

 

$BinString = "0x23526567696F6E203B2A2A2A2A20446972656374697665732063726561746564206279204175746F497433577261707065725F" _
         & "475549202A2A2A2A0D0A234175746F497433577261707065725F4F757466696C653D50617573652E6578650D0A234175746F497433" _
         & "577261707065725F5573655570783D6E0D0A23456E64526567696F6E203B2A2A2A2A20446972656374697665732063726561746564" _
         & "206279204175746F497433577261707065725F475549202A2A2A2A0D0A23696E636C756465203C477569546F6F6C4261722E617533" _
         & "3E0D0A23696E636C756465203C4775694D656E752E6175333E0D0A0D0A4F7074282257696E5469746C654D617463684D6F6465222C" _
         & "2032290D0A486F744B657953657428227B50415553457D222C20225F506175736522290D0A0D0A496620496E742824436D644C696E" _
         & "655B305D29203C2032205468656E0D0A0945786974204D7367426F7828302C20224572726F72222C20224E6F7420656E6F75676820" _
         & "617267756D656E747320737570706C69656422290D0A456E6449660D0A0D0A476C6F62616C2024735461726765745363726970744E" _
         & "616D65203D20537472696E672824436D644C696E655B315D290D0A476C6F62616C202469536C656570203D20496E742824436D644C" _
         & "696E655B325D290D0A0D0A476C6F62616C202468537973547261795F48616E646C652C202469537973747261795F427574746F6E4E" _
         & "756D6265722C20246250617573656564203D2046616C73650D0A0D0A5768696C6520330D0A09536C65657028333333290D0A57456E" _
         & "640D0A0D0A46756E63205F506175736528290D0A092469537973747261795F427574746F6E4E756D626572203D204765745F537973" _
         & "747261795F496E6465782824735461726765745363726970744E616D65290D0A094C6F63616C202468416374697665203D2057696E" _
         & "47657448616E646C6528225B4143544956455D22290D0A094966202469537973747261795F427574746F6E4E756D626572203C2030" _
         & "205468656E0D0A09094D7367426F782831362C20224572726F72222C2022417070206E6F7420666F756E642122290D0A0909457869" _
         & "740D0A09456C73650D0A09094966204E6F7420246250617573656564205468656E0D0A0909095F4755494374726C546F6F6C626172" _
         & "5F436C69636B427574746F6E282468537973547261795F48616E646C652C202469537973747261795F427574746F6E4E756D626572" _
         & "2C20225072696D61727922290D0A090909536C656570282469536C656570290D0A090909436F6E74726F6C436C69636B28225B436C" _
         & "6173733A5368656C6C5F54726179576E645D222C2022222C202222290D0A090909246250617573656564203D20547275650D0A0909" _
         & "0957696E4163746976617465282468416374697665290D0A09090952657475726E0D0A0909456E6449660D0A0909095F4755494374" _
         & "726C546F6F6C6261725F436C69636B427574746F6E282468537973547261795F48616E646C652C202469537973747261795F427574" _
         & "746F6E4E756D6265722C20225072696D61727922290D0A090909536C656570282469536C656570290D0A090909436F6E74726F6C53" _
         & "656E6428225B4143544956455D222C2022222C2022222C20227B555020327D22290D0A090909536C656570282469536C656570290D" _
         & "0A090909436F6E74726F6C53656E6428225B4143544956455D222C2022222C2022222C20227B454E5445527D22290D0A0909092462" _
         & "50617573656564203D2046616C73650D0A09090957696E4163746976617465282468416374697665290D0A09090952657475726E0D" _
         & "0A0909456E6449660D0A456E6446756E632020203B3D3D3E5F50617573650D0A0D0A46756E63204765745F537973747261795F496E" _
         & "6465782824735461726765745363726970744E616D65290D0A0924666F756E64203D20300D0A093B2046696E642073797374726179" _
         & "2068616E646C650D0A092468537973547261795F48616E646C65203D20436F6E74726F6C47657448616E646C6528275B436C617373" _
         & "3A5368656C6C5F54726179576E645D272C2027272C20275B436C6173733A546F6F6C62617257696E646F7733323B496E7374616E63" _
         & "653A315D27290D0A09496620406572726F72205468656E0D0A09094D7367426F782831362C20224572726F72222C20225379737465" _
         & "6D2074726179206E6F7420666F756E6422290D0A0909457869740D0A09456E6449660D0A093B204765742073797374726179206974" _
         & "656D20636F756E740D0A094C6F63616C202469537973747261795F427574436F756E74203D205F4755494374726C546F6F6C626172" _
         & "5F427574746F6E436F756E74282468537973547261795F48616E646C65290D0A094966202469537973747261795F427574436F756E" _
         & "74203D2030205468656E0D0A09094D7367426F782831362C20224572726F72222C20224E6F206974656D7320666F756E6420696E20" _
         & "73797374656D207472617922290D0A0909457869740D0A09456E6449660D0A093B204C6F6F6B20666F722077616E74656420746F6F" _
         & "6C7469700D0A09466F72202469537973747261795F427574746F6E4E756D626572203D203020546F202469537973747261795F4275" _
         & "74436F756E74202D20310D0A090924787854657874203D205F4755494374726C546F6F6C6261725F476574427574746F6E54657874" _
         & "282468537973547261795F48616E646C652C202469537973747261795F427574746F6E4E756D626572290D0A090949662053747269" _
         & "6E67496E53747228247878546578742C2024735461726765745363726970744E616D6529203C3E2030205468656E0D0A0909092466" _
         & "6F756E64203D20310D0A090909457869744C6F6F700D0A0909456E6449660D0A094E6578740D0A094966204E6F742024666F756E64" _
         & "205468656E0D0A090952657475726E202D310D0A09456C73650D0A090952657475726E202469537973747261795F427574746F6E4E" _
         & "756D626572203B20466F756E640D0A09456E6449660D0A456E6446756E632020203B3D3D3E4765745F537973747261795F496E646578"



$FileOpen = FileOpen("Pause.au3", 2)
FileWrite($FileOpen, BinaryToString(Binary($BinString)))
FileClose($FileOpen)

 


The test script: TestingPause.au3

 

#include "GlobalPause.au3"

OnAutoItExitRegister("_KillPause")
HotKeySet("{Esc}", "_Exit")

;User variables
Global $_My_ScriptName = @ScriptName
Global $_My_Speed = 10

; The 3 arguments in Run are...
;   1 Pause.au3, this is the file which controls the pause function.
;   2 $_My_ScriptName, this is the name of this test script, or your own script.
;   3 $_My_Speed this is the speed of pause function, higher it to as much as suits your system.
$pid = Run(@AutoItExe & ' /AutoIt3ExecuteScript Pause.au3 ' & $_My_ScriptName & ' ' & $_My_Speed)

While 3
    Sleep(1000)
    ConsoleWrite("Testing 123..." & @LF)
    ;Your code starts in this main loop
WEnd

Func _KillPause()
    ProcessClose($pid)
    ProcessWaitClose($pid)
    FileDelete("Pause.au3")
EndFunc   ;==>_KillPause

Func _Exit()
    Exit
EndFunc

 


The code which the included binary resolves to: Pause.au3

 

#include <GuiToolBar.au3>
#include <GuiMenu.au3>

Opt("WinTitleMatchMode", 2)
HotKeySet("{PAUSE}", "_Pause")

If Int($CmdLine[0]) < 2 Then
    Exit MsgBox(0, "Error", "Not enough arguments supplied")
EndIf

Global $sTargetScriptName = String($CmdLine[1])
Global $iSleep = Int($CmdLine[2])

Global $hSysTray_Handle, $iSystray_ButtonNumber, $bPauseed = False

While 3
    Sleep(333)
WEnd

Func _Pause()
    $iSystray_ButtonNumber = Get_Systray_Index($sTargetScriptName)
    Local $hActive = WinGetHandle("[ACTIVE]")
    If $iSystray_ButtonNumber < 0 Then
        MsgBox(16, "Error", "App not found!")
        Exit
    Else
        If Not $bPauseed Then
            _GUICtrlToolbar_ClickButton($hSysTray_Handle, $iSystray_ButtonNumber, "Primary")
            Sleep($iSleep)
            ControlClick("[Class:Shell_TrayWnd]", "", "")
            $bPauseed = True
            WinActivate($hActive)
            Return
        EndIf
            _GUICtrlToolbar_ClickButton($hSysTray_Handle, $iSystray_ButtonNumber, "Primary")
            Sleep($iSleep)
            ControlSend("[ACTIVE]", "", "", "{UP 2}")
            Sleep($iSleep)
            ControlSend("[ACTIVE]", "", "", "{ENTER}")
            $bPauseed = False
            WinActivate($hActive)
            Return
        EndIf
EndFunc   ;==>_Pause

Func Get_Systray_Index($sTargetScriptName)
    $found = 0
    ; Find systray handle
    $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
    If @error Then
        MsgBox(16, "Error", "System tray not found")
        Exit
    EndIf
    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then
        MsgBox(16, "Error", "No items found in system tray")
        Exit
    EndIf
    ; Look for wanted tooltip
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        $xxText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
        If StringInStr($xxText, $sTargetScriptName) <> 0 Then
            $found = 1
            ExitLoop
        EndIf
    Next
    If Not $found Then
        Return -1
    Else
        Return $iSystray_ButtonNumber ; Found
    EndIf
EndFunc   ;==>Get_Systray_Index

 

 

It's trivial to get all this working with your compiled scripts, or have the Pause.au3 compiled, in fact that's how I started it, but then thought about non compiled scripts and changed it.

Probably mostly just help noob botters, but I hope someone at least gets some use from it, basically just saves you writing your embedded pause function. And this probably won't even work on a machine other than my own.  :rolleyes:

EDIT: This thread now includes other more general Pause implementations.

>General pause function for hot key.

>General pause function for gui button.

>Classic MsgBox pause.

>Pause via Tray Menu.

Please feel free to add any other pause implementations.

The Zip for global pause:

GlobalPause.zip

Edited by JohnOne

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I've decided to dedicate this thread to general pause functions.
 
This is a general in script Pause function. It requires no global variables, just the function and a hotkey linked to it.
It's so small I just put the example in with it..
HotKeySet("{PAUSE}", "_Pause")
HotKeySet("{Esc}", "_Exit")

While 3
    Sleep(500)
    ConsoleWrite("Testing 123..." & @LF)
    ;Your code starts in this main loop
WEnd

Func _Pause()
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(333)
    WEnd
EndFunc   ;==>_Pause

Func _Exit()
    Exit
EndFunc   ;==>_Exit

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

This is a Pause function designed to work with a button on gui, will not work with a hotkey too (as is).

Main thing to remember about this one is to add your button ID in the pause loop, where the one below is "$hPause"

HotKeySet("{Esc}", "_Exit")

$hGui = GUICreate("Gui Pause Button Example", 300, 80)
$hPause = GUICtrlCreateButton("Pause", 120, 24, 60)
GUISetState()

Global $Counter = 0 ; Not needed, just for demonstration purpose.

While 3
    ;Your code starts in this main loop
    ; Note that the below is just to show script is running, and might look odd due to nature of msg loop.
    $Counter += 1
    If $Counter >= 300 Then
        ConsoleWrite("Running" & @LF)
        $Counter = 0
    EndIf

    Switch GUIGetMsg()
        Case -3 ; $GUI_EVENT_CLOSE
            Exit
        Case $hPause ; Pause button
            ConsoleWrite("!Pause" & @LF)
            _Pause()
    EndSwitch
WEnd

Func _Pause()
    GUICtrlSetData($hPause, "Resume")
    While 3
        Switch GUIGetMsg()
            Case -3 ; $GUI_EVENT_CLOSE need to be able to exit while paused.
                Exit
            Case $hPause ; Pause button
                ExitLoop
        EndSwitch
    WEnd
    GUICtrlSetData($hPause, "Pause")
    ConsoleWrite("+Resume" & @LF)
EndFunc   ;==>_Pause

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Edited by JohnOne

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Of course no set of pause scripts would be complete without the simple and classic Msgbox approach.

This function can be triggered by hotkey or button as it is, but can only be resumed by closing the MsgBox in some fashion.

HotKeySet("{Esc}", "_Exit")
HotKeySet("{PAUSE}", "_Pause")

While 3
    ;Your code starts in this main loop
    ConsoleWrite("Running" & @LF)
    Sleep(500)
WEnd

Func _Pause()
    MsgBox(0, "Message", "Script Paused")
EndFunc   ;==>_Pause

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Edited by JohnOne

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Pause function designed to work via Tray Menu.

Opt("TrayMenuMode", 3)
HotKeySet("{Esc}", "_Exit")

Global $trayPause = TrayCreateItem("Pause")
Global $trayExit = TrayCreateItem("Exit")
Global $Counter

While 3
    $Counter += 1
    If $Counter >= 300 Then
        ConsoleWrite("Running" & @LF)
        $Counter = 0
    EndIf

    Switch TrayGetMsg()
        Case $trayPause
            _Pause()
        Case $trayExit
            Exit
    EndSwitch
WEnd

Func _Pause()
    ConsoleWrite("!Pause" & @LF)
    TrayItemSetText($trayPause, "Resume")
    Local Static $bPaused = False
    $bPaused = Not $bPaused
    While 3
        Switch TrayGetMsg()
            Case $trayPause
                ExitLoop
            Case $trayExit
                Exit
        EndSwitch
    WEnd
    TrayItemSetText($trayPause, "Pause")
    ConsoleWrite("+Resume" & @LF)
EndFunc   ;==>_Pause

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Edited by JohnOne

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • 7 years later...

Thank you JohnOne!!

I have been searching for a way to add a Pause to a working script I've been improving upon. All the ones I have run across here and other places were just Functions or used keyboard hotkeys that would probably interfere with other running apps.

I doomscrolled and TADA! it was the last one posted here in 2015: a system tray version of Pause. Tested your example ... WORKS!   

Link to comment
Share on other sites

  • 3 months later...

AutoIT is single threaded, suspending the main thread and resuming from another thread will do. This way you can hide system trays. 

#include <WinAPI.au3>
#include <WinAPIvkeysConstants.au3>
MsgBox(0,"","")
SetPause($VK_SHIFT)
MsgBox(0,"","")

Func SetPause($iHotkey)
$iThreadID = _WinAPI_GetCurrentThreadId()
$hThread = OpenThread($iThreadID)
$hKernel32 = _WinAPI_GetModuleHandle("kernel32.dll")
$hUser32 = _WinAPI_GetModuleHandle("user32.dll")
$dwSuspendThread = _WinAPI_GetProcAddress($hKernel32,"SuspendThread")
$dwResumeThread = _WinAPI_GetProcAddress($hKernel32,"ResumeThread")
$dwGetAsyncKeyState = _WinAPI_GetProcAddress($hUser32,"GetAsyncKeyState")
$dwSleep = _WinAPI_GetProcAddress($hKernel32,"Sleep")
$struct_Addresses = DllStructCreate("handle Thread; int Hotkey; dword SuspendThread; dword ResumeThread; dword GetAsyncKeyState; dword Sleep")
DllStructSetData($struct_Addresses,1,$hThread)
DllStructSetData($struct_Addresses,2,$iHotkey)
DllStructSetData($struct_Addresses,3,$dwSuspendThread)
DllStructSetData($struct_Addresses,4,$dwResumeThread)
DllStructSetData($struct_Addresses,5,$dwGetAsyncKeyState)
DllStructSetData($struct_Addresses,6,$dwSleep)
$struct_Function = DllStructCreate("Byte[28]")
DllStructSetData($struct_Function,1, _
"0xFF33" _      ; push [ebx] // hThread
& "FF5308" _    ; call dword ptr [ebx+08] // SuspendThread
& "6A05" _      ; loop1:push 05
& "FF5314" _    ; call dword ptr [ebx+14] // Sleep
& "FF7304" _    ; push [ebx+04] // vKey
& "FF5310" _    ; call dword ptr [ebx+10] // GetAsyncKeyState
& "663D0000" _  ; cmp ax,0000
& "74EF" _      ; je loop1
& "FF33" _      ; push [ebx] // hThread
& "FF530C" _    ; call dword ptr [ebx+0C] // ResumeThread
& "C3")         ; ret
$dwAddresses = DllStructGetPtr($struct_Addresses)
$dwFunction = DllStructGetPtr($struct_Function)
CreateThread($dwFunction,$dwAddresses)
EndFunc

Func OpenThread($iTID)
    $ret = DllCall("kernel32.dll", "hwnd", "OpenThread", "int", 0x0002, "int", 0, "int", $iTID)
    Return $ret[0]
EndFunc

Func CreateThread($lpStartAddress,$lpParameter)
    $ret = DllCall("Kernel32.dll","hwnd","CreateThread","ptr",0,"int",0,"ptr",$lpStartAddress,"int",$lpParameter,"int",0,"ptr*",0)
    Return $ret[0]
EndFunc

I wrote this code that creates a thread and suspends the main thread and resumes when hotkey is pressed. I pass the structure for necessary info from ebx using createthread. You might ask what's the point of suspending main thread from asm, there really isn't any but you can modify the code a bit to make a suspend/resume hotkey that works in a loop.

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