Event: OnScriptFinished for ParallelProcessControl
- Updated2024-09-12
- 4 minute(s) read
Events > Event: OnScriptFinished for ParallelProcessControl
Event: OnScriptFinished for ParallelProcessControl
Triggers in parallel processing when a worker object ends the script started with the Run for Worker method. The event starts the user command that you assigned to the OnScriptFinished parameter of the Run for Worker method. This user command must receive one parameter: The parameter contains the Worker object when the user command is called.
Refer to Working with Events in DIAdem for more information on events in DIAdem.
Object.OnWorkerStateChange
Object | ParallelProcessControl Object with this property |
Object.OnWorkerStateChange | Variant with read and write access Specifies the name of the user command. |
The following example starts five Worker objects. The Worker objects execute the WorkerScript.vbs on the current path. After a Worker object has executed the script, it executes the CallbackFct user command in the master.
The Worker objects receive an array as transfer parameter. The content of the first array element is an array, which contains the values of a channel. The second array element contains the ID of the Worker object and the third array element contains the interval width for an RMS calculation.
As soon as a Worker object changes its status, DIAdem executes the CheckState user command. In this user command, you can check, for example, whether an error has occurred. The user commands are defined in the registered Callback.vbs file.
If none of the workers are executing a script, DIAdem deletes all Worker objects. As soon as a worker has completed the WorkerScript.vbs script, DIAdem executes the user command CallbackFct in the master.
Dim i, oMyWorker, MyArgArray(2) Call Data.Root.Clear() Call DataFileLoad("Example.tdm","TDM","") Call ScriptCmdAdd(CurrentScriptPath & "Callback.vbs") ParallelProcessControl.OnWorkerStateChange = "CheckState" For i= 1 to Data.Root.ChannelGroups(2).Channels.Count Set oMyWorker = ParallelProcessControl.Workers.Add(i,true,"") MyArgArray(0) = ChannelsToArray("[2]/[" & i & "]") MyArgArray(1) = i MyArgArray(2) = 50 oMyWorker.SetArgument(MyArgArray) Call oMyWorker.Run(CurrentScriptPath & "WorkerScript.vbs","CallbackFct") Next Do Call Pause(0.1) Loop Until ParallelProcessControl.Workers.RunningWorkersCount < 1 Call ParallelProcessControl.Workers.RemoveAll()
The following script displays the WorkerScript.vbs script mentioned above. The Worker script converts the contents of the first array element back into a channel. The third argument uses the script as the parameter for an RMS calculation. The LocalWorker object transfers the calculation result through the SetArgument for LocalWorker method to the master and the CallbackFct user command evaluates the result. The algorithm is not optimized but is only used as an example for the calculation.
Dim oTempChannel, i, aInputArg, aResults(1) Call DBM ("Worker " & LocalWorker.ID) Call Data.Root.Clear() aInputArg = LocalWorker.GetArgument Call ArrayToChannels(aInputArg(0), Array("tempChannel")) set oTempChannel = Data.GetChannel("/tempChannel") Call RMS(oTempChannel,aInputArg(2)) ' Do some analysis aResults(0) = ChannelsToArray("/tempChannel") aResults(1) = aInputArg(1) Call LocalWorker.SetArgument(aResults) Sub RMS(Channel, Width) Dim i, Sum, j For i = 1 + Width to Channel.Size - Width Sum = 0 For j = i - Width to i Sum = Sum + Channel(j) * Channel(j) Next Channel(i-Width) = Sqr(Sum/Width) Next End Sub
The following script displays the Callback.vbs script mentioned above. The user command CallbackFct displays the ID of the requesting Worker object and evaluates the transfer parameter. In this example the user command replaces the input channels with the calculation results. If the master executes user command instructions, you must ensure that the DIAdem environment matches because, for example, data might have been deleted, layouts changed, or a different panel used after you called the Worker object.
The CheckState user command checks whether an error occurs in the Worker object. If an error occurs, the user command instruction DBM outputs the error description as a debug message and resets the error status.
Sub CallbackFct(oWorker) Dim aResults aResults = oWorker.GetArgument Call DBM("Worker ID: " & oWorker.ID & " Result#: " & aResults(1)) Call ArrayToChannels(aResults(0),array("[2]/[" & aResults(1) & "]"),true) End Sub Sub CheckState(oWorker,iState) ' The following statement is not executed. It is only used for autocompletion. If False Then Set oWorker = ParallelProcessControl.Workers.Add(i,true,"") If oWorker.GetErrorFlag Then DBM "Error: " & oWorker.GetErrorText oWorker.ResetError End If Select Case iState Case eWorkerCreated Call DBM ("Status "& oWorker.ID & " Created") Case eWorkerScriptStarting Call DBM ("Status "& oWorker.ID & " Starting") Case eWorkerScriptFinished Call DBM ("Status "& oWorker.ID & " Finished") Case eWorkerRemoving Call DBM ("Status "& oWorker.ID & " Removing") End Select End Sub