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数据库描述,对于其它关系型数据库也 ...
随机推荐
- CentOS安装wireshark
yum install wireshark-gnome yum install libpcap
- 二 Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器
Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...
- python UDP CS demo
UDP Communication Contents UDP Communication Sending Receiving Using UDP for e.g. File Transfers Mul ...
- 08 - Django应用第五步
1 自动测试 自动测试与测试的不同在于, 自动测试的测试工作是交给系统完成的 测试也有分类和级别, 有的用于一些细微的细节, 有的是针对整个软件整体 测试会保证一些看起来正常运行的功能在实际的多种情况 ...
- C语言小程序(一)、判断三角型类型
最近回炉重造C语言,陆续写一些比较短的代码,选择其中的一些贴到这里,都是在Linux下的代码,Windows未测试. 第一个判断三角形的类型,两个浮点型数据不能直接判断相等,为了输入方便一些,自己设置 ...
- Gym 100801J Journey to the "The World's Start"(二分+单调队列)
题意: 现在有1,2,3...N这N个站, 给定限定时间Limt, N-1种票的价格, 分别对应一个最远距离, 叫你选择一种票, 满足可以在规定时间到达N站台,而且价格最低 思路: 如果买距离为L ...
- Mybatis generator配置文件及说明
项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...
- windows cmd 看服务cpu、内存
开始菜单-运行-cmd-输入systeminfo-回车 不用命令从以下两个地方都可以看出CPU个数 使用命令看CPU 利用win+r键打开运行,输入cmd回车即会出现 查看cpu信息 通过上图可以看出 ...
- [BALTIC 2008] Grid
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1169 [算法] 首先DFS枚举出横着切的 然后二分 + 贪心即可 时间复杂度 : O ...
- UML Design Via Visual Studio-Class Diagram
用过几个建模设计工具,小的有staruml,大的有rational rose,EA.最后发现还是Visual Studio建模比较舒服(个人观点,不要争论). 打算对自己经常用的几个建模图做一个介绍, ...