多进程代替多线程函数 Exec()

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

Post Reply
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

多进程代替多线程函数 Exec()

Post by feiyue » 11 Jan 2019, 08:13

多线程还是很有用的,尤其是在游戏中。这里是一个多进程代替多线程的实现:

Code: Select all

;===========================================
;  【多进程代替多线程函数】 Exec()  By FeiYue
;
;   使用说明:
;   1、启动进程:Exec(代码,进程标记)
;   2、停止进程:Exec("",进程标记)
;   3、进程标记相同,后启动的进程会替换先启动的进程
;   4、主脚本退出时,主脚本启动的所有进程都会被清理
;
;===========================================

Exec(s, flag="Default")  ; By FeiYue
{
  static init
  if (!init)
  {
    init=1
    ss=
    (%
    DetectHiddenWindows, On
    RegExMatch(flag, "<<(.*?)>>", r)
    WinWaitClose, ahk_pid %r1%
    WinGet, list, List, %r% ahk_class AutoHotkeyGUI
    Loop, % list {
      IfEqual, myid, % id:=list%A_Index%, Continue
      WinGet, pid, PID, ahk_id %id%
      WinClose, ahk_pid %pid% ahk_class AutoHotkey
      WinWaitClose, ahk_pid %pid%,, 3
      if ErrorLevel
        Process, Close, %pid%
    }
    )
    Exec(ss, "AutoClear")
  }
  pid:=DllCall("GetCurrentProcessId")
  add=`nflag=<<%pid%>>[%flag%]`n
  (%
    #NoEnv
    #NoTrayIcon
    DetectHiddenWindows, On
    Gui, Gui_Flag_Gui: Show, Hide, %flag%
    Gui, Gui_Flag_Gui: +Hwndmyid
    WinGet, list, List, %flag% ahk_class AutoHotkeyGUI
    Loop, % list {
      IfEqual, myid, % id:=list%A_Index%, Continue
      WinGet, pid, PID, ahk_id %id%
      WinClose, ahk_pid %pid% ahk_class AutoHotkey
      WinWaitClose, ahk_pid %pid%
    }
    DetectHiddenWindows, Off
  )
  s:=add "`n" s "`nExitApp`n#SingleInstance off`n"
  s:=RegExReplace(s, "\R", "`r`n")
  ;-----------------------------------
  shell:=ComObjCreate("WScript.Shell")
  exec:=shell.Exec(A_AhkPath " /ErrorStdOut *")
  exec.StdIn.Write(s)
  exec.StdIn.Close()
}

;======== 下面是使用的例子 ========

;-- 这是一行的写法

F1::Exec("Loop{`nSleep,100`nMouseGetPos,x,y`nToolTip,F1-%A_Index%,x+10,y-30`n}",1)

;-- 这是多行的写法,括号中可以原样粘贴要运行的代码

F2::
s=
(` %
  Loop {
    Sleep, 100
    MouseGetPos, x, y
    ToolTip, F2-%A_Index%, x+10, y+10
  }
)
;-- 使用开关变量来一键切换启动和停止
(ok:=!ok) ? Exec(s,2) : Exec("",2)
return

;-- 清理进程

F3::Exec("",1), Exec("",2)



hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: 多进程代替多线程函数 Exec()

Post by hasantr » 19 Dec 2020, 04:55

谢谢。但这没有用。 F1什么也没做。(Google Translate)

laozhhaiJohn
Posts: 24
Joined: 20 Mar 2021, 18:18

Re: 多进程代替多线程函数 Exec()

Post by laozhhaiJohn » 28 Mar 2021, 07:15

例程经实验确实有效,但暂时还没有脚本用到它。

Post Reply

Return to “脚本函数”