利用 _CoProcSend 让母进程向子进程发送消息[已解决]
本帖最后由 cihren 于 2014-12-1 09:34 编辑近日研究多进程,发现子进程可以母进程发送消息,但母进程却无法向子进程发送消息,不得其解,晒晒:
#include "CoProc.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 354, 118)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("Button1", 16, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("Button1", 128, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button2Click")
$Button3 = GUICtrlCreateButton("Button1", 240, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button3Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
_CoProcReciver("Receiver")
$hA = _CoProc("A")
$hB = _CoProc("B")
$hC = _CoProc("C")
While 1
Sleep(100)
WEnd
Func Button1Click()
MsgBox(0,"","btn1")
_CoProcSend($hA,"AAA") ;按下Button1后向子进程发送消息,无反应!!!
_CloseHandle($hA) ;按下Button1后关闭子进程,无反应!!!
EndFunc
Func Button2Click()
_CoProcSend($hB,"BBB")
EndFunc
Func Button3Click()
_CoProcSend($hC,"CCC")
EndFunc
Func Form1Close()
Exit
EndFunc
Func Receiver($sParama)
MsgBox(0,"Receive Msg",$sParama)
EndFunc
Func A()
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"A","AAAAA",1)
;_CoProcSend($gi_CoProcParent,"From AAA!!") ;这条语句向母进程发送消息,母进行能够收到并显示
WEnd
EndFunc
Func B()
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"B","BBBBB",1)
WEnd
EndFunc
Func C()
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"C","CCCCC",1)
WEnd
EndFunc 研究了一晚上,终于搞定了。问题应该是出在子进程执创建时,只调用指定的函数,对其它代码并不执行,因此也就跳过了开始部份的“_CoProcReciver("Receiver") ”等模块,导到子程序不响应消息。
找到原因后,在子程序中增加“_CoProcReciver("Receiver") ”代码,问题解决~!呵呵!!{:face (356):}Func A()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"A","AAAAA",1)
;_CoProcSend($gi_CoProcParent,$gi_CoProcParent)
WEnd
EndFunc
Func B()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"B","BBBBB",1)
WEnd
EndFunc
Func C()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(5000)
;MsgBox(0,"C","CCCCC",1)
WEnd
EndFunc 楼主你好,可以把解决好的代码发上来吗,最近我也在搞个多进程的需要同一时间处理多项任务的。借鉴一下啊 应A友要求,公布母子进程通讯测试程序全代码,供大家学习。#region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_Res_requestedExecutionLevel=None
#endregion ;**** 参数创建于 ACNWrapper_GUI ****
#include "CoProc.au3"
#include <WinAPIEx.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include 'array.au3'
Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("子母进程通讯测试程序", 354, 118)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("呼叫A进程", 16, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("呼叫B进程", 128, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button2Click")
$Button3 = GUICtrlCreateButton("呼叫C进程", 240, 32, 97, 49)
GUICtrlSetOnEvent(-1, "Button3Click")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
_CoProcReciver("Receiver")
$hA = _CoProc("A")
$hB = _CoProc("B")
$hC = _CoProc("C")
ConsoleWrite("主进程PID:" & @AutoItPID & @CRLF)
ConsoleWrite("子进程PID:" & $hA & " : " & $hB & " : " & $hC & @CRLF)
$list = ProcessList('Autoit3.exe')
_ArrayDisplay($list,"调试代码:各进程PID列表")
While 1
Sleep(100)
WEnd
Func Button1Click()
_CoProcSend($hA, "AAA")
_CloseHandle($hA)
EndFunc ;==>Button1Click
Func Button2Click()
_CoProcSend($hB, "BBB")
EndFunc ;==>Button2Click
Func Button3Click()
_CoProcSend($hC, "CCC")
EndFunc ;==>Button3Click
Func Form1Close()
Exit
EndFunc ;==>Form1Close
Func Receiver($sParama)
MsgBox(0, "进程号:" & @AutoItPID , $sParama)
EndFunc ;==>Receiver
Func A()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
;_CoProcSend($gi_CoProcParent,$gi_CoProcParent)
WEnd
EndFunc ;==>A
Func B()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
WEnd
EndFunc ;==>B
Func C()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
WEnd
EndFunc ;==>C 回复 5# cihren
很好的教材 !
请问 怎么向进度条传递信息呢。请指教! 谢谢!#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
#include "CoProc.au3"
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Global $Progress1
$Form1 = GUICreate("Form1", 625, 443, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Progress1 = GUICtrlCreateProgress(72, 88, 486, 17)
$Button1 = GUICtrlCreateButton("进度", 96, 272, 75, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("显示信息1", 288, 280, 75, 25, 0)
GUICtrlSetOnEvent(-1, "Button2Click")
$Button3 = GUICtrlCreateButton("退出", 480, 272, 75, 25, 0)
GUICtrlSetOnEvent(-1, "Button3Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
_CoProcReciver("Receiver")
$hA = _CoProc("A")
$hB = _CoProc("B")
$hC = _CoProc("C")
While 1
Sleep(1000)
WEnd
Func Button1Click()
_CoProcSend($hA, "AAA")
_CloseHandle($hA)
EndFunc ;==>Button1Click
Func Button2Click()
_CoProcSend($hB, "BBB")
EndFunc ;==>Button2Click
Func Button3Click()
;_CoProcSend($hC, "CCC")
Form1Close()
EndFunc ;==>Button3Click
Func Form1Close()
Exit
EndFunc ;==>Form1Close
Func Receiver($sParama)
MsgBox(0, "进程号:" & @AutoItPID , $sParama)
EndFunc ;==>Receiver
Func A()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
;_CoProcSend($gi_CoProcParent,$gi_CoProcParent)
WEnd
EndFunc ;==>A
Func B()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
WEnd
EndFunc ;==>B
Func C()
_CoProcReciver("Receiver")
While ProcessExists($gi_CoProcParent)
Sleep(200)
WEnd
EndFunc ;==>C 回复 6# laomeng
本由重点是母进程向子进程发送消息,更多用法请参考:http://www.autoitx.com/forum.php?mod=viewthread&tid=9961&highlight=%BD%F8%B3%CC
页:
[1]