在C#中多次运行一个exe命令的最快的方法是什么? [英] What is the fastest way to run an exe command many times in C#?

查看:1504
本文介绍了在C#中多次运行一个exe命令的最快的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码(用C#编写)运行 exe 命令多次(平均800次)。

My code (written in C#) runs an exe command many times (800 times average).

目前我在C#中运行 exe 命令作为进程

Currently I run the exe command as a Process in C#:

  var process1 = new Process()
  {
      StartInfo = new ProcessStartInfo()
      {
          FileName = "latex",
          Arguments = String.Format("-quiet -output-directory=\"{0}\" \"{1}\"", equationDirectory, equationTEX),
          WorkingDirectory = equationDirectory,
          CreateNoWindow = true,
          UseShellExecute = false,
          RedirectStandardError = true,
          RedirectStandardOutput = true
      }
  };
  process1.Start();

这需要花费这么多时间,有些是Windows启动shell进程。

This is taking so much time, some of it is the Windows starting the shell process.

问题
我想知道是否更快嵌入 exe 在我的代码并运行它?
什么是最快的方式来运行可执行文件多次(在循环中说)?

Question I was wondering if it is faster to embed the exe in my code and run it? What is the fastest way to run an executable many times (in a loop let say)?

推荐答案

也许使用类似下面的东西,并测试可同时运行的数字,您会发现一个甜点:

Maybe using something like below, and testing the number that can run concurrently, you'll find a sweet-spot:

var processes = new List<Process>();

var process1 = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "latex",
        Arguments = String.Format("-quiet -output-directory=\"{0}\" \"{1}\"", equationDirectory, equationTEX),
        WorkingDirectory = equationDirectory,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardOutput = true
    }
};

//Add all of your processes to a list before actually running them
processes.Add(process1);

//This will run 5 in parallel
Parallel.ForEach(processes, new ParallelOptions { MaxDegreeOfParallelism = 5 }, p => { p.WaitForExit(); });

这篇关于在C#中多次运行一个exe命令的最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆