我的代码基本上是这样的:如何同时运行多个“任务”?
for each (DirectoryInfo di in directoryList)
{
for each (FileInfo fi in di.GetFiles())
{
MyTask(fi.FullName);
Console.WriteLine(fi.FullName + " is done.");
}
}
void MyTask(string arg0)
{
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "converter.exe";
p.StartInfo.Arguments = "-converterarguments";
p.Start();
p.WaitForExit();
}
如何让我的程序在同一时间运行多个MyTask/converter.exe的“实例”?
为什么不使用任务?另外,诀窍是让你的MyTask返回进程,所以你可以多次调用它,然后等待所有的进程。 –
注意你写的代码。你应该为它们创建一个数组,并且不要调用'p.WaitForExit()',直到你把所有的'Start()'都编辑完成。 –
你真的想分开*进程吗?你确定在你想要实现的单个程序中不是单独的*线程吗? – Jamiec