重新加载脚本时如何在AutoHotKey上启动无限循环?

时间:2016-12-05 18:37:41

标签: autohotkey

重新加载脚本时如何在AutoHotKey上启动无限循环?

当我的AutoHotKey脚本被加载时,我想启动一个看门狗来监视我的Octave的Plot窗口何时打开。这样,脚本将最大化窗口。

但是当我重新加载它时,这个脚本没有启动,即没有显示Hi消息框。

#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

这是脚本之前的代码:

NumpadDot::.
Return

F1::F2
Return

RCtrl::RAlt
Return

#Persistent ; uncomment this line to see the effect
SetTimer, Hello, 1000 ; go to lable hello every second

Hello:
MsgBox Hi
Return


#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

;
^+8::
Run "D:\User\Documents\AutoHotKey\MyBatches\kill_macro_player.vbs"

参考文献:

  1. https://autohotkey.com/docs/commands/SetTitleMatchMode.htm
  2. https://autohotkey.com/docs/commands/WinWait.htm
  3. https://autohotkey.com/docs/commands/WinMaximize.htm
  4. https://autohotkey.com/docs/commands/_Persistent.htm

1 个答案:

答案 0 :(得分:0)

@Jim U评论之后,我发现要解决此问题,将脚本添加为文件中的第一个内容:

#persistent

SetTitleMatchMode 2
Loop
{
    MsgBox Hi
    WinWait, Figure 1
    WinMaximize
    Sleep 1000
}

Return

然而, AutoHotKey 应用程序开始使用我的CPU的 2%,而不是像脚本中这些行之前的 0.00%。然后我将WinWait, Figure 1仅替换为WinMaximize Figure 1 AutoHotKey 应用程序开始使用我的CPU处理能力的 0.08%,这要好得多。

#persistent

SetTitleMatchMode 1
Loop
{
    WinMaximize Figure 1
    Sleep 1000
}

Return

关于@Robert Ilbrink评论:

  

看起来您的代码以NumpadDot ::开头。和回归。 AHK将自动启动任何已加载的脚本,直到它遇到的第一个返回。一旦AHK找到返回,所有其他命令(在第一次返回之后)仅在被触发时执行(例如,热键)。

     

而不是Sleep,我会使用启动标记脚本的计时器(SetTimer [,Label,Period | On | Off | Delete,Priority])。所以不要循环,而是每1000毫秒启动一次标记的脚本。

#persistent

SetTimer, check_for_windows, 1000
SetTitleMatchMode 1

check_for_windows:
{
    ; MsgBox, Hello
    WinMaximize Figure 1
}

Return

然而,使用SleepSetTimer CPU的两种方式保持相同,但SetTimer似乎消耗 0.09%而不是我的CPU使用率 0.08%

参考文献:

  1. https://autohotkey.com/docs/commands/SetTimer.htm
  2. https://autohotkey.com/board/topic/40986-what-is-wintext/
  3. https://autohotkey.com/board/topic/63955-can-you-please-give-a-simple-example-of-persistent/
  4. General Handler

    我创建了一个更通用的脚本处理程序,现在可以为更多程序设置它。在这里我评论了WinWait命令,因为他们吃了很多CPU,如上所述。

    ; Declare it to be alive all the time.
    #persistent
    
    ; Create the initial task to start working.
    SetTimer, check_for_sublime_settings_window, 1000
    SetTimer, check_for_octave_graphics_window, 1000
    
    ; Set to window's title must start with the specified WinTitle to be a match.
    SetTitleMatchMode 1
    
    ; Stop the initial configuration setup, i.e., this execution flow.
    Return
    
    check_for_sublime_settings_window:
    {
        ; Here we wait until the windows is activated and only then to maximize it.
        ;WinWait, Preferences.sublime-settings
    
        ; If we do not wait for it, it will bring the windows up every time we minimize it.
        WinMaximize, Preferences.sublime-settings
    
        ; Set to wait for the next time the windows open within 1 seconds delay.
        SetTimer, check_for_sublime_settings_window, 1000
    }
    ; To stop this execution flow
    Return
    
    
    check_for_octave_graphics_window:
    {
        ;WinWait, Figure 1
        WinMaximize, Figure 1
        SetTimer, check_for_octave_graphics_window, 1000
    }
    Return