Reputation: 95
I have a folder containing multiple PCL files which need converting to PDF. I am able to achieve this using a third party exe. To speed this up I am trying to run multiple Tasks(), each which starts a new System.Diagnostics.Process using the exe;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = $@".\WinPCLtoPDF\WinPCLtoPDF.exe";
process.StartInfo.Arguments = $@"{StepParameters["StagingFileName"]} {StepParameters["StagingFileName"]}.pdf batch";
process.Start();
process.WaitForExit();
The tasks are added to a List<Task>
and each is awaited before the program exits.
foreach (FileInfo fileInfo in files)
{
tasks.Add(ProcessDocumentTaskAsync(batchType, fileInfo, deleteOriginalFile));
while (tasks.Count < files.Count() && tasks.Where(x => !x.IsCompleted).Count() > concurrentTasks)
{
Thread.Sleep(50);
}
}
The Tasks are created using a method like this.
private async static Task ProcessDocumentTaskAsync(BatchType batchType, FileInfo fileInfo, bool deleteOriginalFile)
{
await Task.Run(() =>
{
ProcessParameters processParameters = ProcessParams();/////get process params
DocumentProcessor documentProcessor = GetDocumentProcessor(batchType, processParameters);
using (documentProcessor)
{
documentProcessor.ProcessDocument();
}
});
}
This pattern works for other tasks and you can see from a log file that jobs are being performed asynchronously. However, with this WinPCLtoPDF.exe, it appears to only process one file at a time, yet the task manager shows there are multiple processes running. Process 1 & 2 for example will wait, while 3 starts and finishes and is replaced by 4, 5 etc, until finally the whole folder is process and 1 then 2 complete.
Can I find out why 1 & 2 are appear to be being blocked and do not complete quickly, allowing other tasks to start?
Upvotes: 1
Views: 4597
Reputation: 592
The easiest solution is probably (as mentioned in a comment) to use Parallel.ForEach
. In your case it would look something like:
Parallel.ForEach(files, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, fileInfo =>
{
ProcessParameters processParameters = ProcessParams();/////get process params
DocumentProcessor documentProcessor = GetDocumentProcessor(batchType, processParameters);
using (documentProcessor)
{
documentProcessor.ProcessDocument();
}
});
Note on your trying:
The reason it is not running all the Tasks at once is probably because you are awaiting the Task completion in your ProcessDocumentTaskAsync
function.
Upvotes: 0