In the case of C # Program optimization, multi-process scenarios are also used if the multithreading effect is poor:
System.Threading.Tasks.Task task=System.Threading.Tasks.Task.Factory.StartNew ( (Object mystate) = { = Process.Start ("AutoCollectMrMultipleProcess.exe" ) , MyState. ToString ()); Process. WaitForExit (); } , Collectpathitems.dequeue ())
Use process in C # to open a thread and run a C # compiled Business Project for a console, Console.exe determines that each process in a parallel process handles specific tasks by receiving parameters: for example, to implement a multi-process download, the parameters passed to each process. exe are the paths to be collected.
Generally open up the process of the task number is also to be limited, such as the number of opening process and the number of computer cores as Enviroment.processcount. So here's the problem.
Question one: How do you ensure that the number of processes running at the same time in a C # business code ensures that the maximum number of processes is maximized?
Assume: We have 25 tasks under task, some tasks are 1 hours to complete, some 10 minutes to complete, how to ensure a complete business code to ensure that after 10 minutes to complete, found that the current number of processes has not reached the maximum number, and still need to deal with the task, continue to open up new download process tasks.
Issue two: The maximum number of processes mentioned above also contains the number of processes opened by other users in the computer.
Assuming a: Has opened 3 AutoCollectMrMultipleProcess.exe, User B to carry out their own acquisition tasks, the number of processes allowed to open up: enviroment.processcount-3 (if the value is already less than or equal to 0, Will no longer open up, into the waiting).
Question three: How to ensure that business processes are synchronized.
Implementation code:
intMaxprocesscount =Enviroment.processcount; List<System.Threading.Tasks.Task> TaskItems =NewList<system.threading.tasks.task>(); Queue<string> collectpathietms=new queue<string> ();
Initializing the download task record start
。。。
Initializing the download task record end
intcursor =0; while(! (Collectpathitems.count = =0&& Taskitems.count = =0)) { foreach(System.Threading.Tasks.Task TaskIteminch NewList<system.threading.tasks.task>(TaskItems)) { if(Taskitem.status = =System.Threading.Tasks.TaskStatus.Canceled|| Taskitem.status = =System.Threading.Tasks.TaskStatus.Faulted|| Taskitem.status = =System.Threading.Tasks.TaskStatus.RanToCompletion) {taskitems.remove (Taski TEM); } } //if Collectpathitems.count = = 0, no new tasks will be added, so there is no need to execute the rest of the code below. //instead, just wait for the top task to finish jumping out of the loop. if(Collectpathitems.count = =0) {Thread.Sleep (5* -* +); Continue; } process[] ProcessItems= Process.getprocessesbyname ("autocollectmrmultipleprocess"); if(Processitems.length >=Maxprocesscount) {Thread.Sleep (5* -* +); Continue; } intDequeuecount = ((maxprocesscount-processitems.length) > Collectpathitems.count)? Collectpathitems.count: (Maxprocesscount-processitems.length); for(inti =0; i < Dequeuecount; i++) {Taskitems.add (System.Threading.Tasks.Task.Factory.StartNew ( (ObjectMyState) ={Process Process= Process.Start ("AutoCollectMrMultipleProcess.exe", MyState. ToString ()); Process. WaitForExit (); }, Collectpathitems.dequeue ())); }
Sleep seconds ... Thread.Sleep (30* +); Cursor++; }
C #: Multi-process development, controlling the number of processes