How to set AutoHotKey process priority & radom sle

2019-08-29 02:43发布

问题:

I want to set the high priority on the script below (only first).

Another important thing is whether you can do a Radom Sleep, 250 to 350??

Loop
{
    {
        ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, mor.png
        bT:= ErrorLevel ? bT : 1
        If bT
        {
            bT:= 0
            Random, x, 1130, 1300
            Random, y, 580, 680
            Click % x % % y %
                Sleep, 500

        }
    }

    ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, biz.png
    bT:= ErrorLevel ? bT : 1
    {
        bT:= 0
        Random, x, 540, 618
        Random, y, 419, 430
        Click % x % % y %

                 }
}
Return
f1:: ExitApp

回答1:

Try it this way:

Process, Priority,, High 

Loop
{
    If GetKeyState("F1","P")
        ExitApp
    ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, mor.png
    bT:= ErrorLevel ? bT : 1
    If bT
    {
        bT:= 0
        Random, x, 1130, 1300
        Random, y, 580, 680
        Click %x%, %y%
        Sleep, % rnd(250,350)   
    }
    ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, biz.png
    bT:= ErrorLevel ? bT : 1
    If bT
    {
        bT:= 0
        Random, x, 540, 618
        Random, y, 419, 430
        Click %x%, %y%
    }   
}

rnd(min,max){
    Random, myVar,% min,% max
    return myVar
}

Untested.



回答2:

If you want to change the priority of the thread the script is running on so that it can/cannot be interrupted by another thread, you use the Thread command:

Thread, Priority, Level

Where Level is a signed 32 bit integer. See AHK — Thread.

You can generate a pseudo-random number using the Randomcommand (as you already are in your code). If you want to pause the thread using random numbers, you do it like so:

Random, timer, 250, 350    ; Generates the pseudo-random number
Sleep, %timer%               ; Pauses script execution according to value