如何限制启动的进程数?
How to limit the numbers of processes to start?
我写了一个程序,它使用 Process.Start 调用 cmd.exe
调用 qaac.exe
将歌曲转换为 aac。
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
有效,但一次只能转换 1 首歌曲。现在我想让它一次总是隐藏 4 首歌曲(运行 一次处理 4 个过程)。
好像有4个新进程。如果一个退出,开始一个新的。始终在后台填充4个进程。
怎么做?
@帕特里克霍夫曼
谢谢!有用!
但是我遇到了另一个问题。
我在下面重写了我的代码:
Parallel.ForEach(commands,
new ParallelOptions() {MaxDegreeOfParallelism = 4 },
command => trans_aac(command));
static void trans_aac(string command) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = command;
proc.EnableRaisingEvents = true;
proc.Exited += ProcessExited;
proc.Start();
while (!eventHandled) {
}
eventHandled = false;
}
static void ProcessExited(object sender, EventArgs e) {
eventHandled = true;
}
因为while循环,使得转换的速度变得非常慢。
有没有更好的方法让每个进程等待自己退出?
最简单的方法就是使用 Task Parallel Framework's Parallel.ForEach
。可以设置最大同时执行数:
string[] commands = new string[] { "mspaint", "notepad" };
Parallel.ForEach
( commands
, new ParallelOptions() { MaxDegreeOfParallelism = 4 }
, command => RunOneTask(command)
);
用命令填充数组或List<string>
,并将其传递给方法。您应该将当前代码放入一个单独的方法 RunOneTask
中,该方法接受一个参数:要执行的命令。
我找到了解决问题的方法。不过不知还有没有更聪明的。
static int i=0
static void send_covert_cmd() {
if (i < commands.Count) {
trans_aac(commands[i]);
i++;
}
}
//Start 4 process
sendcommand();
sendcommand();
sendcommand();
sendcommand();
static void trans_aac(string command) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = command;
proc.EnableRaisingEvents = true;
proc.Exited += ProcessExited;
proc.StartInfo.CreateNoWindow = false;
proc.Start();
}
static void ProcessExited(object sender, EventArgs e) {
send_covert_cmd();
}
我写了一个程序,它使用 Process.Start 调用 cmd.exe
调用 qaac.exe
将歌曲转换为 aac。
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
有效,但一次只能转换 1 首歌曲。现在我想让它一次总是隐藏 4 首歌曲(运行 一次处理 4 个过程)。
好像有4个新进程。如果一个退出,开始一个新的。始终在后台填充4个进程。
怎么做?
@帕特里克霍夫曼 谢谢!有用! 但是我遇到了另一个问题。 我在下面重写了我的代码:
Parallel.ForEach(commands,
new ParallelOptions() {MaxDegreeOfParallelism = 4 },
command => trans_aac(command));
static void trans_aac(string command) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = command;
proc.EnableRaisingEvents = true;
proc.Exited += ProcessExited;
proc.Start();
while (!eventHandled) {
}
eventHandled = false;
}
static void ProcessExited(object sender, EventArgs e) {
eventHandled = true;
}
因为while循环,使得转换的速度变得非常慢。 有没有更好的方法让每个进程等待自己退出?
最简单的方法就是使用 Task Parallel Framework's Parallel.ForEach
。可以设置最大同时执行数:
string[] commands = new string[] { "mspaint", "notepad" };
Parallel.ForEach
( commands
, new ParallelOptions() { MaxDegreeOfParallelism = 4 }
, command => RunOneTask(command)
);
用命令填充数组或List<string>
,并将其传递给方法。您应该将当前代码放入一个单独的方法 RunOneTask
中,该方法接受一个参数:要执行的命令。
我找到了解决问题的方法。不过不知还有没有更聪明的。
static int i=0
static void send_covert_cmd() {
if (i < commands.Count) {
trans_aac(commands[i]);
i++;
}
}
//Start 4 process
sendcommand();
sendcommand();
sendcommand();
sendcommand();
static void trans_aac(string command) {
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = command;
proc.EnableRaisingEvents = true;
proc.Exited += ProcessExited;
proc.StartInfo.CreateNoWindow = false;
proc.Start();
}
static void ProcessExited(object sender, EventArgs e) {
send_covert_cmd();
}