-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPC-Example-ManualHandling.au3
More file actions
118 lines (104 loc) · 5.36 KB
/
IPC-Example-ManualHandling.au3
File metadata and controls
118 lines (104 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.18.0
Author: Kanashius
Script Function:
Example script for the IPC InterProcessCommunication UDF.
This example shows how to manually handle data between the processes.
#ce ----------------------------------------------------------------------------
#include "IPC.au3"
#include <EditConstants.au3>
Global Const $iCOMMAND_TEST = 1, $iCOMMAND_UNKNOWN = 2, $iCOMMAND_PROGRESS = 3
Global $mMainGui[] ; just a map for all ctrl variables to avoid to many global variables
; make sure main process pullrate is 0 to disable automatic data pulling for the main process
__IPC_StartUp($__IPC_LOG_INFO, 0)
; check if the call is a sub process and start the respective function
; make sure sub process pullrate is 0 to disable automatic data pulling for the sub process
Global $hProcess = __IPC_SubCheck("_SubProcess", "_MainProcess", "_CallbackSub", "_CallbackSubClose", $__IPC_LOG_INFO, 0)
If @error Then __IPC_Log($__IPC_LOG_ERROR, "__IPC_SubCheck: "&@error&":"&@extended)
; main/sub process both should call shutdown before exit
__IPC_Shutdown()
Exit
; the main process main method, registered in __IPC_SubCheck to be called when the script is running as main process (no sub process command line arguments detected)
Func _MainProcess()
Local Static $hSubProcessLast = 0
Local $iWidth = 800, $iHeight = 600, $iCtrlHeight = 25, $iSpace = 5
$mMainGui.hGui = GUICreate("Example IPC", $iWidth, $iHeight)
Local $iButtonWidth = ($iWidth-4*$iSpace)/3
$mMainGui.idButtonStart = GUICtrlCreateButton("Start subprocess", $iSpace, $iSpace, $iButtonWidth, $iCtrlHeight)
$mMainGui.idButtonStop = GUICtrlCreateButton("Stop subprocess", $iSpace*2+$iButtonWidth, $iSpace, $iButtonWidth, $iCtrlHeight)
$mMainGui.idButtonPause = GUICtrlCreateButton("Pause handling", $iSpace*3+$iButtonWidth*2, $iSpace, $iButtonWidth, $iCtrlHeight)
Local $iTop = $iCtrlHeight+$iSpace*2
$mMainGui.idProgress = GUICtrlCreateProgress($iSpace, $iTop, $iWidth-2*$iSpace, $iCtrlHeight)
$iTop += $iCtrlHeight+$iSpace*2
$mMainGui.idEdit = GUICtrlCreateEdit("", $iSpace, $iTop, $iWidth-2*$iSpace, $iHeight-$iTop-$iSpace, BitOR($ES_READONLY, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
GUISetState()
Local $bPauseProcessHandling = False
While True
Switch GUIGetMsg()
Case -3
ExitLoop
Case $mMainGui.idButtonStart
GUICtrlSetData($mMainGui.idEdit, "")
GUICtrlSetData($mMainGui.idProgress, 0)
$hSubProcessLast = __IPC_StartProcess("_CallbackMain", "100", "_CallbackSubProcessEnds")
Case $mMainGui.idButtonStop
If $hSubProcessLast<>0 Then __IPC_ProcessStop($hSubProcessLast)
Case $mMainGui.idButtonPause
$bPauseProcessHandling = Not $bPauseProcessHandling
If $bPauseProcessHandling Then GUICtrlSetData($mMainGui.idButtonPause, "Resume handling")
If Not $bPauseProcessHandling Then GUICtrlSetData($mMainGui.idButtonPause, "Pause handling")
EndSwitch
If Not $bPauseProcessHandling Then
; handle incoming data at the main process, manual pulling
__IPC_MainProcessing()
EndIf
WEnd
EndFunc
; registered as callback in __IPC_StartProcess to be called when data from the sub process is received
Func _CallbackMain($hSubProcess, $iCmd, $arData)
Switch $iCmd
Case $iCOMMAND_TEST
GUICtrlSetData($mMainGui.idEdit, "COMMAND_TEST ["&$hSubProcess&"]: "&$arData[0]&@crlf, True)
Case $iCOMMAND_PROGRESS
Local $iTotal = $arData[0]
Local $iItemsDone = $arData[1]
Local $dProgress = $iItemsDone/$iTotal
Local $iPerc = Int($dProgress*100)
GUICtrlSetData($mMainGui.idProgress, $iPerc)
GUICtrlSetData($mMainGui.idEdit, "COMMAND_PROGRESS ["&$hSubProcess&"]: "&$iItemsDone&"/"&$iTotal&" = "&Round($dProgress, 2)&" => "&$iPerc&"%"&@crlf, True)
Case Default
GUICtrlSetData($mMainGui.idEdit, $arData[0]&@crlf, True)
Case Else
GUICtrlSetData($mMainGui.idEdit, "COMMAND_UNKNOWN ["&$hSubProcess&"] ["&$iCmd&"] ["&UBound($arData)&"]"&@crlf, True)
EndSwitch
EndFunc
; registered as callback in __IPC_StartProcess to be called when a sub process is closed/ends
Func _CallbackSubProcessEnds($hSubProcess)
__IPC_Log($__IPC_LOG_INFO, "SUBPROCESS ENDS ["&$hSubProcess&"]")
EndFunc
; the sub process main method, registered in __IPC_SubCheck to be called when the script is running as a sub process
Func _SubProcess($hSubProcess)
Local $iTotalItems = 10
If UBound($CmdLine)>1 Then $iTotalItems = Int($CmdLine[1])
__IPC_SubSend("Start processing items") ; send data without a command
For $i=0 to $iTotalItems-1
__IPC_SubSendCmd($iCOMMAND_PROGRESS, $iTotalItems, $i+1)
If @error Then Return SetError(2, 0 , False)
Sleep(Random(1,500, 1))
Next
__IPC_SubSend("Done processing items")
__IPC_SubSendCmd($iCOMMAND_TEST, "test command")
If @error Then __IPC_Log($__IPC_LOG_ERROR, "Failed sending", @error, @extended) ; to check for errors when sending
__IPC_SubSendCmd($iCOMMAND_UNKNOWN) ; send an unknown command
; handle incoming data at the sub process, manual pulling
__IPC_SubProcessing()
Return True
EndFunc
; registered as callback in __IPC_SubCheck to be called, when the connection to the main process is seperated
Func _CallbackSubClose()
__IPC_Log($__IPC_LOG_INFO, "Stop processing items and terminate sub process")
EndFunc
; registered as callback in __IPC_SubCheck to be called when data from the main process is received
Func _CallbackSub($iCmd, $arData)
ConsoleWrite("Callback Sub: "&$iCmd&" >> "&UBound($arData)&@crlf)
EndFunc