Autoit里用多进程模拟多线程

 
一直以来Autoit都不支持多线程,因此一些需要同时运行多个循环的操作也就无法实现。这个问题在其它的某些语言里也经常出现,解决的方法就是使用多进程。 所谓多进程,就是同时运行多个子进程,每个子进程负责不同的操作,藉此达到和多线程相当的效果。Autoit本身已经具备了实现多进程的条件,且已经有人完成了相关的自定义函数。下面我将具体讲解如何利用这些自定义函数实现多进程。 首先到 http://www.autoitscript.com/foru ... 29326&hl=CoProc 下载CoProc.zip,压缩包里的CoProc.au3包含了实现多进程的相关函数,你可以把这个文件复制到Include目录下。 函数使用说明: _CoProc() 这个函数的作用是启动自己的子进程,然后执行指定的函数。比方说如果你想另开一个进程执行“fuck”函数,代码写_CoProc("fuck")就行了。这个函数会返回子进程的PID,在对子进程进行操作时会用到这个PID。另外,你可以无限制开启子进程,且每个子进程都可以建立完全独立的GUI。 _ProcSuspend() & _ProcResume() 暂停/恢复进程。如果你开了一个子进程专门下载文件,就可以利用这两个函数暂停/继续下载。范例:_ProcSuspend(@PID) 。 _CloseHandle() 关闭子进程。范例:_CloseHandle(@PID) 。 _CoProcSend() 向指定进程发送消息。当子进程有新的信息(比如完成下载)需要提示母进程时,就可以使用这个函数。范例:_CoProcSend($PID, "发送的消息内容")。 _CoProcReciver() 注册一个函数用于接收其他进程用_CoProcSend函数传递过来的消息,每当收到消息时注册的函数就会被执行。需要注意的是,被注册的函数必须有且只有一个自定义参数,而传递的参数里就是发送过来的消息。范例:_CoProcReciver("函数名称")。 例子:
  1. #NoTrayIcon
  2. #include "CoProc.au3"
  3. #region 主程序区域
  4. #include <GUIConstants.au3>
  5. $Form1 = GUICreate("Multiple File Download", 622, 119, 192, 125)
  6. GUICtrlCreateLabel("http://www.mv.com/test/paths.txt (1Mb)", 8, 8)
  7. GUICtrlCreateLabel("http://support.shaw.ca/troubleshooting/files/download.dat (20Mb)", 8, 48)
  8. $Progress1 = GUICtrlCreateProgress(8, 24, 601, 17)
  9. $Progress2 = GUICtrlCreateProgress(8, 64, 601, 17)
  10. $Button1 = GUICtrlCreateButton("Pause", 8, 88, 81, 25)
  11. $Button2 = GUICtrlCreateButton("Resume", 96, 88, 81, 25)
  12. $iPidSmall = _CoProc("Small") ;开启子进程,子进程将执行Small()函数,$iPidSmall得到的是子进程的PID
  13. $iPidBig = _CoProc("Big")
  14. GUISetState(@SW_SHOW)
  15. _CoProcReciver("Reciver") ;注册Reciver()函数来接收子进程传递过来的消息
  16. While 1
  17.     $msg = GuiGetMsg()
  18.     Select
  19.     Case $msg = $GUI_EVENT_CLOSE
  20.         ExitLoop
  21.     Case $msg = $Button1
  22.           _ProcSuspend($iPidSmall) ;暂停$iPidSmall这个子进程
  23.           _ProcSuspend($iPidBig)
  24.     Case $msg = $Button2
  25.           _ProcResume($iPidSmall) ;恢复$iPidSmall子进程
  26.           _ProcResume($iPidBig)
  27.     Case Else
  28.         ;
  29.     EndSelect
  30. WEnd
  31. FileDelete(@TempDir & "smalltest.tmp")
  32. FileDelete(@TempDir & "bigtest.tmp")
  33. Exit
  34. Func Reciver($vParameter)
  35.     ;$vParameter里就是子进程发来的消息
  36.     $aParam = StringSplit($vParameter,"|")
  37.     If $aParam[1] = "small" Then GUICtrlSetData($Progress1,$aParam[2])
  38.     If $aParam[1] = "big" Then GUICtrlSetData($Progress2,$aParam[2])
  39. EndFunc
  40. #endregion
  41. #region Small()函数里是'Small file'子进程的所要执行的代码
  42. Func Small()
  43.     $url = "http://www.mv.com/test/paths.txt"
  44.     $size = InetGetSize($url)
  45.     InetGet($url,@TempDir & "smalltest.tmp",1,1)
  46.     While @InetGetActive And ProcessExists($gi_CoProcParent)
  47.         ;在下载时不断向父进程发送下载进度,$gi_CoProcParent是父进程的PID,这个变量是函数自己建立的
  48.           _CoProcSend($gi_CoProcParent,"small|" & Round(@InetGetBytesRead / $size * 100,0))
  49.         Sleep(250)
  50.     WEnd
  51.       _CoProcSend($gi_CoProcParent,"small|100")
  52. EndFunc
  53. #endregion
  54. #region 'Big file'子进程执行的代码
  55. Func Big()
  56.     $url = "http://support.shaw.ca/troubleshooting/files/download.dat"
  57.     $size = InetGetSize($url)
  58.     InetGet($url,@TempDir & "bigtest.tmp",1,1)
  59.     While @InetGetActive And ProcessExists($gi_CoProcParent)
  60.           _CoProcSend($gi_CoProcParent,"big|" & Round(@InetGetBytesRead / $size * 100,0))
  61.         Sleep(250)
  62.     WEnd
  63.       _CoProcSend($gi_CoProcParent,"big|100")
  64. EndFunc
  65. #endregion
复制代码
注意事项: 子进程发送消息时需要母进程的PID,而母进程的PID储存在$gi_CoProcParentli里 子进程可以正常使用脚本里的所有自定义函数 在子进程执行的那个函数里你不能再#include函数库或是用Func定义函数 对一个子进程不要重复使用_ProcSuspend()和_ProcResume() 函数,否则会让子进程无响应
原文地址:https://www.cnblogs.com/u0mo5/p/3973728.html