1
我嘗試使用Parallel.For
來運行進程並獲得並行輸出。Process StandardOutput ReadToEnd多個進程得到空/空字符串
示例代碼:
internal class Program
{
private static void Main(string[] args)
{
var bag = new ConcurrentBag<string>();
Parallel.For(0, int.MaxValue, i =>
{
bag.Add(MyMethod());
});
}
public static string MyMethod()
{
using (var a = new Process())
{
a.StartInfo.FileName = "A.exe";
a.StartInfo.RedirectStandardError = true;
a.StartInfo.RedirectStandardInput = true;
a.StartInfo.RedirectStandardOutput = true;
a.StartInfo.CreateNoWindow = true;
a.StartInfo.UseShellExecute = false;
a.StartInfo.ErrorDialog = false;
a.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
a.Start();
string output = a.StandardOutput.ReadToEnd();
a.WaitForExit();
return output; // sometime output will be null
}
}
}
A.exe時碼
internal class Program
{
private static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 40; j++)
{
Console.Write("A");
}
Console.Write(Environment.NewLine);
}
}
}
有誰知道爲什麼會輸出空,我怎麼能避免獲得空的結果?
您是否嘗試過'a.SystemError'流? – VMAtm