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数据库描述,对于其它关系型数据库也 ...
随机推荐
- hdu-5652 India and China Origins(二分+bfs判断连通)
题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 20179203李鹏举 《Linux内核原理与分析》第一周学习笔记
Linux基础入门 一.Linux的基础学习 1.1 Linux的重要基础操作 Linux不同于Windows的纯粹的图形化界面,虽然也有图形桌面的操作但是更多的操作还是通过命令行来进行,当然除了命令 ...
- SQL Server DBA十大必备工具使生活轻松
[IT168 技术]曾经和一些DBA和数据库开发人员交流时,问他们都用过一些什么样的DB方面的工具,大部分人除了SSMS和Profile之外,基本就没有使用过 其他工具了;诚然,SSMS和Profil ...
- C#实现根据传入时间段,找出时间段内日期,并生成相对应文件路径
[1]获取固定日期范围内的所有日期,以数组形式返回 /// <summary> /// 获取固定日期范围内的所有日期,以数组形式返回 /// </summ ...
- asp.net中日志框架Log4Net的使用
Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过日志查看系统的运行过程,从而发现系统的问题.日志的作用:将运 ...
- Poj 2017 Speed Limit(水题)
一.Description Bill and Ted are taking a road trip. But the odometer in their car is broken, so they ...
- 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果
目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...
- 人物-IT-雷军:雷军
ylbtech-人物-IT-雷军:雷军 雷军 (全国工商联副主席,小米科技创始人.董事长) 雷军,1969年12月16日出生于湖北仙桃,毕业于武汉大学,是中国大陆著名天使投资人. 雷军作为中国互联网 ...
- ss2
一. *** 服务端配置 1. 在命令行窗口输入下面4行命令并回车执行 yum -y update yum install -y python-setuptools && easy_i ...
- Python-实现与metasploit交互并进行ms17_010攻击
关于ms17_010,可参考http://www.cnblogs.com/sch01ar/p/7672454.html 目标IP:192.168.220.139 本机IP:192.168.220.14 ...