执行队列
Execution Queue
我需要通过外部程序处理 100 条信息。这些过程可能很密集,所以我需要将其限制为一次处理 8 个。本质上,我想启动 8 个进程,每个进程完成后,启动下一个进程。
我尝试使用下面的代码在 System.Threading.Tasks.Dataflow 中使用 TPL,但是全部 100 次启动,而不是一次只启动 8 次。
// This file has the command line parameters to launch the external process
List<string> lines = File.ReadAllLines(file).ToList();
var block = new ActionBlock<string>(async job => await RunJob(job), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 });
foreach (string line in lines)
{
block.SendAsync(line);
}
static async Task RunJob(string parms)
{
//Console.WriteLine("PARMS: {0}", parms);
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.WindowStyle = ProcessWindowStyle.Normal;
start.FileName = @"C:\program.exe";
string parameters = String.Format(parms.ToString());
start.Arguments = parameters;
start.UseShellExecute = true;
proc.StartInfo = start;
proc.Start();
}
我错过了什么?感谢您的帮助。
流程立即开始,但您不必等到流程结束。使用 proc.WaitForExit();
static async Task RunJob(string parms)
{
//Console.WriteLine("PARMS: {0}", parms);
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.WindowStyle = ProcessWindowStyle.Normal;
start.FileName = @"C:\program.exe";
string parameters = String.Format(parms.ToString());
start.Arguments = parameters;
start.UseShellExecute = true;
proc.StartInfo = start;
proc.Start();
proc.WaitForExit();
}
我需要通过外部程序处理 100 条信息。这些过程可能很密集,所以我需要将其限制为一次处理 8 个。本质上,我想启动 8 个进程,每个进程完成后,启动下一个进程。
我尝试使用下面的代码在 System.Threading.Tasks.Dataflow 中使用 TPL,但是全部 100 次启动,而不是一次只启动 8 次。
// This file has the command line parameters to launch the external process
List<string> lines = File.ReadAllLines(file).ToList();
var block = new ActionBlock<string>(async job => await RunJob(job), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 });
foreach (string line in lines)
{
block.SendAsync(line);
}
static async Task RunJob(string parms)
{
//Console.WriteLine("PARMS: {0}", parms);
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.WindowStyle = ProcessWindowStyle.Normal;
start.FileName = @"C:\program.exe";
string parameters = String.Format(parms.ToString());
start.Arguments = parameters;
start.UseShellExecute = true;
proc.StartInfo = start;
proc.Start();
}
我错过了什么?感谢您的帮助。
流程立即开始,但您不必等到流程结束。使用 proc.WaitForExit();
static async Task RunJob(string parms)
{
//Console.WriteLine("PARMS: {0}", parms);
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.WindowStyle = ProcessWindowStyle.Normal;
start.FileName = @"C:\program.exe";
string parameters = String.Format(parms.ToString());
start.Arguments = parameters;
start.UseShellExecute = true;
proc.StartInfo = start;
proc.Start();
proc.WaitForExit();
}