用CoProc写了个简单测试的,运行不稳定,不知道哪里的问题
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUICtrlOnHover.au3>
#include <ProgressConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstants.au3>
#include <Excel.au3>
#include <string.au3>
#include <Process.au3>
#include <File.au3>
#include <Array.au3>
#include <Date.au3>
#include <Timers.au3>
#include <WinAPIFiles.au3>
#include <WinAPI.au3>
#include <ACN_NET.au3>
#include <Constants.au3>
#include <GuiEdit.au3>
#include <GuiRichEdit.au3>
#include "CoProc.au3"
#RequireAdmin
Global $GUI, $list, $hQuery, $aRow, $txt5, $total, $VER, $ST_button, $Form1
Global $Group[5], $G1[5], $G11[5], $Edit[5], $Lab1[5], $Lab2[5], $Input[5], $CommFlag[5], $resOpen[5], $setport[5], $COM[5], $CommFlag[5], $SendFlag[5], $ReceiveFlag[5], $ComEN[5]
Global $iniFile, $SelectFile, $Form22
$Form1 = GUICreate("Test", 1300, 720, -1, -1)
$p = 320
For $i = 1 To 4 Step 1
$Group[$i] = GUICtrlCreateGroup("", 16 + $p * ($i - 1), 72, 300, 620)
$Lab1[$i] = GUICtrlCreateLabel("串口连接", 42 + $p * ($i - 1), 103, 120, 31)
GUICtrlSetFont(-1, 15, 800, 0, "微软雅黑")
$COM[$i] = GUICtrlCreateLabel("", 142 + $p * ($i - 1), 103, 80, 31)
GUICtrlSetFont(-1, 15, 800, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x0000CD)
$G1[$i] = GUICtrlCreateGraphic(240 + $p * ($i - 1), 100, 40, 40)
GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 1) ; - - 设置描边线粗细
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000, 0x808080) ; - - 设置描边填充颜色
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 5, 5, 30, 30) ; - - 设置形状大小
$G11[$i] = GUICtrlCreateGraphic(240 + $p * ($i - 1), 100, 90, 40)
GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 1) ; - - 设置描边线粗细
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000, 0x00FF00) ; - - 设置描边填充颜色
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 5, 5, 30, 30) ; - - 设置形状大小
GUICtrlSetState(-1, $GUI_HIDE)
$Edit[$i] = _GUICtrlRichEdit_Create($Form1, "", 22 + $p * ($i - 1), 160, 280, 400, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_SetFont(-1, 16, "Arial")
$Lab2[$i] = GUICtrlCreateLabel("测试结果", 22 + $p * ($i - 1), 590, 120, 31)
GUICtrlSetFont(-1, 15, 800, 0, "微软雅黑")
$Input[$i] = GUICtrlCreateInput("", 22 + $p * ($i - 1), 630, 280, 40, 0x0001)
GUICtrlSetFont(-1, 20, 400, 0, "Arial")
GUICtrlSetBkColor(-1, 0xA9A9A9)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Next
$ST_button = GUICtrlCreateButton("", 1500, 15, 5, 5)
GUICtrlSetFont(-1, 12, 800, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x0000CD)
$dll = DllOpen("user32.dll")
Dim $AccelKeys[1][2] = [["{Space}", $ST_button]]
GUISetAccelerators($AccelKeys)
GUISetAccelerators($AccelKeys)
GUISetState(@SW_SHOW)
_CoProcReciver("Reciver")
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
_Commcloseport(True)
ExitLoop
Case $ST_button
Initial_Gui()
$iPid1= _CoProc("send1")
$iPid2= _CoProc("send2")
$iPid3= _CoProc("send3")
$iPid4= _CoProc("send4")
EndSwitch
WEnd
Func Reciver($vParameter)
;$vParameter里就是子进程发来的消息
$aParam = StringSplit($vParameter,"|")
If $aParam[1] = "1" Then WriteLogData($aParam[2], 1)
If $aParam[1] = "2" Then WriteLogData($aParam[2], 2)
If $aParam[1] = "3" Then WriteLogData($aParam[2], 3)
If $aParam[1] = "4" Then WriteLogData($aParam[2], 4)
EndFunc
Func Initial_Gui()
For $i = 1 To 4 Step 1
_GUICtrlRichEdit_SetText($Edit[$i], "")
GUICtrlSetData($Input[$i], "")
Next
EndFunc ;==>Initial_Gui
Func WriteLogData($data, $nE)
_GUICtrlEdit_AppendText($Edit[$nE], $data & @LF)
EndFunc ;==>WriteLogData
Func send1()
For $i=1 To 10
_CoProcSend( $gi_CoProcParent,'1' & '|' & $i & '-test' & @LF)
Sleep(1000)
Next
EndFunc
Func send2()
For $j=1 To 10
_CoProcSend( $gi_CoProcParent,'2' & '|' & $j & '-test' & @LF)
Sleep(1000)
Next
EndFunc
Func send3()
For $m=1 To 10
_CoProcSend( $gi_CoProcParent,'3' & '|' & $m & '-test' & @LF)
Sleep(1000)
Next
EndFunc
Func send4()
For $n=1 To 10
_CoProcSend($gi_CoProcParent,'4' & '|' & $n & '-test' & @LF)
Sleep(1000)
Next
EndFunc
|