This example requires basic knowledge of the TPL (Task parallel library). Microsoft documentation
provide good introductional articles about asynchronous programming. We recommend to use the async/await pattern wherever possible.
In order to demonstrate parallel execution, we need a network to run. In this example, we will create 5 simple object in memory. Then we will sent it into a RowTransformation, where we write the record details to the console output, delay execution by 10 ms and then write the record into a xml file. This is the code to create our demo network:
Now we have basically two options to execute a network: Execute or ExecuteAsync.
The Execute method is a shortcut for calling wait on the returned Task from the ExecuteAsync method: ExecuteAsync().GetAwaiter().GetResult() would be the equivalent of Execute().
So we will only show examples here that use the ExecuteAsync() method. The ExecuteAsync method return a task object - this task will complete when all components in the network have finished.
Our first example shows what happens if you use the await keyword to wait for the completion of each network. The output of this example is very similar to a synchronous execution, but it won’t block execution if there are other tasks running in your program code.
The next example shows if we use the task objects returned from each ExecuteAsync method and wait for all of them simultanously. This will execute the networks in parallel.
The last example shows how to wait for two networks without using await. We can use the Task.WaitAll() method to wait for completion of both networks. WaitAll will block execution until both networks are complete, but the networks themselves will both run in parallel.