Before getting into the solution, Datatable or datarow are not thread safe. If you are doing any data modification to data in a datarow in your validation using parallel tasks, you will face issues.
I would suggest to use Tasks instead of BackgroundWorker. First let us try to understand how Parallel.Invoke works.
From MSDN for Parallel.Invoke:
Quote:
No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.
Quote:
Note that with Invoke, you simply express which actions you want to run concurrently, and the runtime handles all thread scheduling details, including scaling automatically to the number of cores on the host computer.
So, what happens in your code is, you are splitting the datatble of 50000 records into 5 datatables of 10000 each. There will be 5 parallel tasks, each having 10000 records to validate, sequentially. This is the reason why you did not see any improvement in performance.
The better way to do this will be, to process number of records equal to the number of cores in your host machine. Max can be 2 times number of cores.
Example: if your machine has 4 cores, you can process 8 records max at a time to get the best of the performance.
The next question will be, Do you have to create 50000/8 = 6250 datattables before calling Parallel.Invoke? Yes because this has to be done on way or the other and No because you do not have to do this before calling Parallel.Invoke.
Here is some sample code to show how it can be done, but not the only way. This code is not tested.
long recordsProcessed = 0;
int interval = 8;
While(recordsProcessed <= totalCount)
{
arrayObj[0] = MyTable.AsEnumerable().Skip(0).Take(interval).CopyToDataTable();
recordsProcessed += 8;
}
Once you do this, your Validate method should be change to take DataRow instead of a datatable.
blExeM.Validate(dataRowToValidate, contries, ref result, selectedContries);
You can also use Parallel.ForEach or PLINQ instead of Parallel.Invoke. Search for these terms if you want to learn more.
Finally, to make the UI responsive, the method that calls the Validate method has to invoked asynchronously.