In order to perform multiple tasks in parallel, you can start multiple processes (number of parallel).
The following two methods are available, total task Number 10, max parallel number 4.
First, Method 1
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Threading;namespaceprocess Parallel { Public classStartProcess1 {intTotalprocess =Ten;//number of total task intMaxparallelprocess =4;//maximum number of concurrent processes intCurrunningprocess =0;//number of currently running processes Public voidDo () {DoEvents (); } /// <summary> ///Execution Process/// </summary> Private voidDoEvents () { for(inti =0; i < totalprocess; i++) {ProcessStartInfo ProcessInfo=NewProcessStartInfo (); Processinfo.filename=@"c \ process. exe"; Processinfo.arguments= (i +1). ToString (); Process Pro=NewProcess (); Pro. EnableRaisingEvents=true; Pro. StartInfo=ProcessInfo; Pro. Exited+=NewEventHandler (process_exited); Pro. Start (); //Pro. WaitForExit (18000);currunningprocess++; //waits for the process to exit if it is greater than the maximum number of parallel digits, which is the maximum number of parallel while(Currunningprocess >=maxparallelprocess) { if(I >= totalprocess-1) { return; } } } } /// <summary> ///Process End/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidProcess_exited (Objectsender, EventArgs e) {currunningprocess--; } }}
Second, Method 2
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Threading;namespaceprocess Parallel { Public classStartProcess2 {Static intTotalprocess =Ten;//number of total task Static intMaxparallelprocess =4;//maximum number of concurrent processes Static intDoneprocess =0;//number of processes that have been executed Static intTopro =0;//number of processes started for the first time Public voidDo2 () {//totalprocess processes are started when the number of total task is less than the number of process parallelismTopro = totalprocess < maxparallelprocess?totalprocess:maxparallelprocess; for(inti =0; i < Topro; i++) {doneprocess++; DoEvents2 (); } //All tasks are executed before the main process is finished while(Doneprocess <totalprocess) { } } /// <summary> ///Execution Process/// </summary> Private voidDoEvents2 () {ProcessStartInfo ProcessInfo=NewProcessStartInfo (); Processinfo.filename=@"c \ process. exe"; Processinfo.arguments=(doneprocess). ToString (); Process Pro=NewProcess (); Pro. EnableRaisingEvents=true; Pro. StartInfo=ProcessInfo; Pro. Exited+=NewEventHandler (PROCESS_EXITED2); Pro. Start (); //Pro. WaitForExit (18000)//wait up to 18 seconds to exit the process } /// <summary> ///Process End/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidProcess_exited2 (Objectsender, EventArgs e) {doneprocess++; Topro--;//the first four processes to start are not fully started if(Doneprocess <=totalprocess) {DoEvents2 ();//end a process, and then start a process } } }}
Iii. process
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Threading;namespaceProcess {classProgram {Static voidMain (string[] args) {Console.WriteLine (string. Format ("This is the first {0} process", args)); Thread.Sleep ( the); } }}
Iv. User Invocation
<window x:class="process parallelism. MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"height=" -"Width="525"> <Window.Resources> <style targettype="Button"> <setter property="Width"Value=" -"></Setter> <setter property="Height"Value=" -"></Setter> <setter property="HorizontalAlignment"Value="Center"></Setter> <Style.Triggers> <trigger property="IsMouseOver"Value="true"> <setter property="Background"Value="Blue"></Setter> </Trigger> </Style.Triggers> </Style> </windo w.resources> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <button click="Button_click_1"> method 1</button> <button grid.row="1"click="button_click_2"> Methods 2</button> </Grid></Window>usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceprocess Parallel {/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); } /// <summary> ///Method 1/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton_click_1 (Objectsender, RoutedEventArgs e) {StartProcess1 ProStart1=NewStartProcess1 (); Prostart1.do (); } /// <summary> ///Method 2/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton_click_2 (Objectsender, RoutedEventArgs e) {StartProcess2 ProStart2=NewStartProcess2 (); Prostart2.do2 (); } }}
C # multi-process parallelism