Skip to main content

Hello
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

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; };
this.OrderList.SetProcessDelegate<SOOrderEntry>(Process);

 


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


Reply