随笔 - 49  文章 - 0 评论 - 19 
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

Web Site Traffic Counter

常用链接

留言簿(3)

随笔分类(48)

随笔档案(49)

搜索

  •  

积分与排名

  • 积分 - 60947
  • 排名 - 99

最新评论

阅读排行榜

评论排行榜

源处:http://hi.baidu.com/zhch_ao/blog/item/6025f7354eff9889a61e1247.html
一直以来Autoit都不支持多线程,因此一些需要同时运行多个循环的操作也就无法实现。这个问题在其它的某些语言里也经常出现,解决的方法就是使用多进程。

所谓多进程,就是同时运行多个子进程,每个子进程负责不同的操作,藉此达到和多线程相当的效果。Autoit本身已经具备了实现多进程的条件,且已经有人完成了相关的自定义函数。下面我将具体讲解如何利用这些自定义函数实现多进程。

首先到http://www.autoitscript.com/forum/index.php?showtopic=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("函数名称")。

例子:

#NoTrayIcon
#include "CoProc.au3"

#region 主程序区域
#include <GUIConstants.au3>
$Form1 = GUICreate("Multiple File Download", 622, 119, 192, 125)
GUICtrlCreateLabel("http://www.mv.com/test/paths.txt (1Mb)", 8, 8)
GUICtrlCreateLabel("http://support.shaw.ca/troubleshooting/files/download.dat (20Mb)", 8, 48)
$Progress1 = GUICtrlCreateProgress(8, 24, 601, 17)
$Progress2 = GUICtrlCreateProgress(8, 64, 601, 17)
$Button1 = GUICtrlCreateButton("Pause", 8, 88, 81, 25)
$Button2 = GUICtrlCreateButton("Resume", 96, 88, 81, 25)

$iPidSmall = _CoProc("Small") ;开启子进程,子进程将执行Small()函数,$iPidSmall得到的是子进程的PID
$iPidBig = _CoProc("Big")
GUISetState(@SW_SHOW)

_CoProcReciver("Reciver") ;注册Reciver()函数来接收子进程传递过来的消息

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Button1
         _ProcSuspend($iPidSmall) ;暂停$iPidSmall这个子进程
         _ProcSuspend($iPidBig)
    Case $msg = $Button2
         _ProcResume($iPidSmall) ;恢复$iPidSmall子进程
         _ProcResume($iPidBig)
    Case Else
        ;
    EndSelect
WEnd
FileDelete(@TempDir & "\smalltest.tmp")
FileDelete(@TempDir & "\bigtest.tmp")
Exit

Func Reciver($vParameter)
    ;$vParameter里就是子进程发来的消息
    $aParam = StringSplit($vParameter,"|")
    If $aParam[1] = "small" Then GUICtrlSetData($Progress1,$aParam[2])
    If $aParam[1] = "big" Then GUICtrlSetData($Progress2,$aParam[2])
EndFunc
#endregion  

#region Small()函数里是'Small file'子进程的所要执行的代码
Func Small()
    $url = "http://www.mv.com/test/paths.txt"
    $size = InetGetSize($url)
    InetGet($url,@TempDir & "\smalltest.tmp",1,1)
    While @InetGetActive And ProcessExists($gi_CoProcParent)
        ;在下载时不断向父进程发送下载进度,$gi_CoProcParent是父进程的PID,这个变量是函数自己建立的
         _CoProcSend($gi_CoProcParent,"small|" & Round(@InetGetBytesRead / $size * 100,0))
        Sleep(250)
    WEnd
     _CoProcSend($gi_CoProcParent,"small|100")
EndFunc
#endregion

#region 'Big file'子进程执行的代码
Func Big()
    $url = "http://support.shaw.ca/troubleshooting/files/download.dat"
    $size = InetGetSize($url)
    InetGet($url,@TempDir & "\bigtest.tmp",1,1)
    While @InetGetActive And ProcessExists($gi_CoProcParent)
         _CoProcSend($gi_CoProcParent,"big|" & Round(@InetGetBytesRead / $size * 100,0))
        Sleep(250)
    WEnd
     _CoProcSend($gi_CoProcParent,"big|100")
EndFunc
#endregion

注意事项:
  • 子进程发送消息时需要母进程的PID,而母进程的PID储存在$gi_CoProcParentli里
  • 子进程可以正常使用脚本里的所有自定义函数
  • 在子进程执行的那个函数里你不能再#include函数库或是用Func定义函数
  • 对一个子进程不要重复使用_ProcSuspend()和_ProcResume() 函数,否则会让子进程无响应
posted on 2008-01-15 12:07 疾风随影 阅读(6236) 评论(0)  编辑 收藏 引用 所属分类: AutoIT column
只有注册用户登录后才能发表评论。