首页 > 代码库 > C#多进程并行

C#多进程并行

为了并行执行多个任务,可以启动多个进程(并行数)。

下面提供两种方法,总任务数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=http://www.mamicode.com/"50"></Setter>
            <Setter Property="Height" Value=http://www.mamicode.com/"50"></Setter>
            <Setter Property="HorizontalAlignment" Value=http://www.mamicode.com/"Center"></Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value=http://www.mamicode.com/"true">
                    <Setter Property="Background" Value=http://www.mamicode.com/"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#多进程并行