小编Mak*_*bra的帖子

产生过程,但一次只有5个

我正在使用文件名工作.每个文件都必须由外部二进制文件处理.这工作正常,但它一次只处理一个文件.是否有可能两个并行产生多个进程?

Queue<string> queue = new Queue<string>();
queue.Enqueue("1.mp3");
queue.Enqueue("2.mp3");
queue.Enqueue("3.mp3");
...
queue.Enqueue("10000.mp3");

while (queue.Count > 0)
{
    string file = queue.Dequeue();

    Process p = new Process();    
    p.StartInfo.FileName = @"binary.exe";
    p.StartInfo.Arguments = file;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    p.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)

更新:我喜欢Alex LE的解决方案(Spawn进程,但一次只有5个),但是可以按照Ben Voigt的建议等待子进程退出吗?

编辑1:我需要检查p.ExitCode == 0来进行一些数据库更新.

c# asynchronous process console-application

6
推荐指数
1
解决办法
1849
查看次数

标签 统计

asynchronous ×1

c# ×1

console-application ×1

process ×1