Using MultiThreading for Import Orders

  • 22 July 2021
  • 2 replies

I am facing below issue when i am trying to  Prepare Orders using the Multi Threading

Sample Code :

            var availableTasks = Environment.ProcessorCount - 1;
            List<List<KCAPIOrder>> objliUsrMappingBatches = SplitIntoChunks(orders, ((orders.Count / availableTasks) + 1));
            Task[] tasks = new Task[availableTasks];

            for (int x = 0; x < availableTasks ; x++)
                int index = x;

                tasks[index] = new Task(() =>
                    foreach (KCAPIOrder order in objliUsrMappingBatches[index])
                        GetOrderDetails(store, request, order);
                }, cancellationToken);
            tasks.StartAndWaitAll(cancellationToken, logger);


Can anyone Help on this


Best answer by Gabriel Michaud 22 July 2021, 20:44

View original

2 replies

Userlevel 6
Badge +8

This happens because you are trying to use the same connection from multiple threads at once; this is not possible.

Wrapping each task into its own PXConnectionScope might work, but I would suggest leveraging the (undocumented) support for parallel processing that’s available in PXProcessing.

In web.config, make the following changes:

<add key="ParallelProcessingDisabled" value="false" />
<add key="AsyncNumbering" value="true" />
<add key="EnableAutoNumberingInSeparateConnection" value="true" />

Ensure your processing delegate looks like that (important - only specific overrides of SetProcessDelegate support multi-threading):

public static void Process(PXGraph graph, KCAPIOrder Order)
//Do your work here

Finally, set the delegate this way during initialization:

//Replace OrderList with the name of your PXProcessing object and OrderEntry with the target graph that you're writing to.
this.OrderList.ParallelProcessingOptions = settings => { settings.IsEnabled = true; settings.BatchSize = 100; };


Thanks @Gabriel Michaud I implemented yourchanges and now its working fine


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2020  Acumatica, Inc. All rights reserved