Good afternoon, dear community.
Please conduct a code review and give advice. There is a program. It needs to run the same external EXE file N times with different input parameters. Wait until all the running processes are completed and collect the results into a single, say, array.
How to use parallelism and asynchrony (if the latter is needed here) with maximum efficiency?
I have so far made such a decision:
List<Tuple<int, int, string, string>> processignDataList = ... //ΡΠΏΠΈΡΠΎΠΊ ΠΊΠΎΡΡΠ΅ΠΆΠ΅ΠΉ Ρ Π²Ρ
ΠΎΠ΄Π½ΡΠΌΠΈ Π΄Π°Π½Π½ΡΠΌΠΈ Task[] stackTasks = new Task[processignDataList.Count]; // ΠΌΠ°ΡΡΠΈΠ² Π²ΡΠ΅Ρ
ΡΠ°ΡΠΊΠΎΠ² List<Tuple<int, string>> tasksResults = new List<Tuple<int, string>>(); //ΡΠΏΠΈΡΠΎΠΊ ΠΊΠΎΡΡΠ΅ΠΆΠ΅ΠΉ Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°ΠΌΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠΊΠΈ ΡΠ°ΡΠΊΠΎΠ² int i = 0; // ΡΡΠ΅ΡΡΠΈΠΊ, Π½ΡΠΆΠ΅Π½ ΡΠΎΠ»ΡΠΊΠΎ Π΄Π»Ρ ΡΠΊΠ°Π·Π°Π½ΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π° foreach (Tuple<int, int, string, string> pair in processignDataList) { stackTasks[i] = Task.Factory.StartNew(() => tasksResults.Add(Tuple.Create(pair.Item1, pairProcessing(cSettings, pair)))); i++; } Task.WaitAll(stackTasks); The string pairProcessing() method:
- takes properties from the special object
cSettings, input parameters from pair, - Runs EXE as Process -
process.Start(); - there is also a limit to the endless hangup -
process.WaitForExit(3000) - then check that
process.Close(); StandardOutputAnalysis, and Based on It- forming a certain string and its return as a result of the work of the
pairProcessingmethod
Do I achieve with this solution run multiple copies of EXE and parallel execution? Are there any additional pairProcessing() to the pairProcessing() method, are there any requirements for it, or can it be any? Do you know the solutions better? (and the solution is always better ...) Are there any shoals? Thank.