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 = ;//总任务数
int maxParallelProcess = ;//并行最大进程数
int curRunningProcess = ;//当前运行进程数
public void Do()
{
DoEvents();
} /// <summary>
/// 执行进程
/// </summary>
private void DoEvents()
{
for (int i = ; i < totalProcess; i++)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = @"C:\进程.exe";
processInfo.Arguments = (i + ).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 - )
{ 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 = ;//总任务数
static int maxParallelProcess = ;//并行最大进程数
static int doneProcess = ;//已经执行完的进程数
static int toPro = ;//第一次启动的进程数
public void Do2()
{
//当总任务数小于进程并行数时启动totalProcess个进程
toPro = totalProcess < maxParallelProcess ? totalProcess : maxParallelProcess;
for (int i = ; 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();
}
}
}
四、用户调用
<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="" Width="">
<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>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Click="Button_Click_1">方法1</Button>
<Button Grid.Row="" 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#多进程并行的更多相关文章
- python多进程并行代码
from multiprocessing import Process import sys, os import time def timetask(string): while True: pri ...
- {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll
Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...
- Python多线程多进程那些事儿看这篇就够了~~
自己以前也写过多线程,发现都是零零碎碎,这篇写写详细点,填一下GIL和Python多线程多进程的坑~ 总结下GIL的坑和python多线程多进程分别应用场景(IO密集.计算密集)以及具体实现的代码模块 ...
- (数据科学学习手札70)面向数据科学的Python多进程简介及应用
本文对应脚本已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 一.简介 进程是计算机系统中资源分配的最小单位,也是操作系 ...
- 深入浅析python中的多进程、多线程、协程
深入浅析python中的多进程.多线程.协程 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源 ...
- Python中多线程与多进程的恩恩怨怨
概念: 并发:当有多个线程在操作时,如果系统只有一个CPU,则它根本不可能真正同时进行一个以上的线程,它只能把CPU运行时间划分成若干个时间段,再将时间 段分配给各个线程执行,在一个时间段的线程代码运 ...
- [源码解析] PyTorch分布式优化器(2)----数据并行优化器
[源码解析] PyTorch分布式优化器(2)----数据并行优化器 目录 [源码解析] PyTorch分布式优化器(2)----数据并行优化器 0x00 摘要 0x01 前文回顾 0x02 DP 之 ...
- 字符型图片验证码识别完整过程及Python实现
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- 数据库访问性能优化 Oracle
特别说明: 1. 本文只是面对数据库应用开发的程序员,不适合专业DBA,DBA在数据库性能优化方面需要了解更多的知识: 2. 本文许多示例及概念是基于Oracle数据库描述,对于其它关系型数据库也 ...
随机推荐
- hbase_异常_02_hbase无法访问16010端口
一.异常现象 上一个异常解决了之后,已经能正常启动hbase了,也能正常使用hbase shell ,但是无法通过浏览器访问 16010端口. 二.异常原因 1.原因一 hbase 1.0 以后的版 ...
- MySQL--开发技巧(一)
Inner Join: Left Outer Join: Right Outer Join: Full Join: Cross Join: SELECT t1.attrs ,t2.attrs FROM ...
- 5_Singleton 游戏开发中的单例模式
强制类只有一个实例 提供全局的访问 ###为什么使用: ``` 如果没有地方访问这个类,则不会创建实例 静态类在main之前实例化, 可以尝试Lazy initialization 派生单例类, 获得 ...
- mongodb与mysql的命令对比
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- c/c++写的比较好的读写配置文件的函数或者类
共用版 .h文件 //---------------------------------------------------------------------------- // 程序名称: ...
- dubbo monitor simple 监控原理分析
监控机制: 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心.监控中心负责统计各服务调用次数,调用时间等. 监控元数据存储目录结构: --dubbo.jetty ...
- ACM学习历程—SNNUOJ1132 余数之和(数论)
Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...
- Oracle 12C 新特性之在线重命名、迁移活跃的数据文件
Oracle 数据库 12c 版本中对数据文件的迁移或重命名不再需要太多繁琐的步骤,可以使用 ALTER DATABASE MOVE DATAFILE 这样的 SQL 语句对数据文件进行在线重命名和移 ...
- JQUERYUI 框架 http://jqueryui.com/
http://jqueryui.com/
- JavaScript-Tool:my97datepicker
ylbtech-JavaScript-Tool:my97datepicker 1.返回顶部 1. 2.下载 https://files.cnblogs.com/files/storebook/java ...