C#多進程並行
阿新 • • 發佈:2017-07-23
等待 program thread ack event art windows click png
為了並行執行多個任務,可以啟動多個進程(並行數)。
下面提供兩種方法,總任務數10,最大並行數4。
一、方法1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Threading; namespace 進程並行 { public class StartProcess1 { int totalProcess = 10;//總任務數 int maxParallelProcess = 4;//並行最大進程數 int curRunningProcess = 0;//當前運行進程數 public void Do() { DoEvents(); } /// <summary> /// 執行進程 /// </summary> private void DoEvents() { for (int i = 0; i < totalProcess; i++) { ProcessStartInfo processInfo= new ProcessStartInfo(); processInfo.FileName = @"C:\進程.exe"; processInfo.Arguments = (i + 1).ToString(); Process pro = new Process(); pro.EnableRaisingEvents = true; pro.StartInfo = processInfo; pro.Exited+= new EventHandler(process_Exited); pro.Start(); //pro.WaitForExit(18000); curRunningProcess++; //如果大於最大並行數,就等待進程退出,是並行數不超過最大並行數 while (curRunningProcess >= maxParallelProcess) { if (i >= totalProcess - 1) { return; } } } } /// <summary> /// 進程結束 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void process_Exited(object sender, EventArgs e) { curRunningProcess--; } } }
二、方法2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Threading; namespace 進程並行 { public class StartProcess2 { static int totalProcess = 10;//總任務數 static int maxParallelProcess = 4;//並行最大進程數 static int doneProcess = 0;//已經執行完的進程數 static int toPro = 0;//第一次啟動的進程數 public void Do2() { //當總任務數小於進程並行數時啟動totalProcess個進程 toPro = totalProcess < maxParallelProcess ? totalProcess : maxParallelProcess; for (int i = 0; i < toPro; i++) { doneProcess++; DoEvents2(); } //任務全部執行後再結束主進程 while (doneProcess < totalProcess) { } } /// <summary> /// 執行進程 /// </summary> private void DoEvents2() { ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.FileName = @"C:\進程.exe"; processInfo.Arguments = (doneProcess).ToString(); Process pro = new Process(); pro.EnableRaisingEvents = true; pro.StartInfo = processInfo; pro.Exited += new EventHandler(process_Exited2); pro.Start(); //pro.WaitForExit(18000)//等待最多18秒退出進程 } /// <summary> /// 進程結束 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void process_Exited2(object sender, EventArgs e) { doneProcess++; toPro--;//第一次要啟動的四個進程還沒有完全啟動 if (doneProcess <= totalProcess) { DoEvents2();//結束一個進程,再啟動一個進程 } } } }
三、進程
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Threading; namespace 進程 { class Program { static void Main(string[] args) { Console.WriteLine(string.Format("這是第{0}個進程", args)); Thread.Sleep(3000); } } }
四、用戶調用
<Window x:Class="進程並行.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="Button"> <Setter Property="Width" Value="50"></Setter> <Setter Property="Height" Value="50"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="Blue"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Click="Button_Click_1">方法1</Button> <Button Grid.Row="1" Click="Button_Click_2">方法2</Button> </Grid> </Window> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace 進程並行 { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 方法1 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_1(object sender, RoutedEventArgs e) { StartProcess1 proStart1 = new StartProcess1(); proStart1.Do(); } /// <summary> /// 方法2 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_2(object sender, RoutedEventArgs e) { StartProcess2 proStart2 = new StartProcess2(); proStart2.Do2(); } } }
C#多進程並行