コンソール アプリケーション、Windows 10、Visual Studio 2015、.NET 4.6 では、メインで TestProcess という単一のメソッドを呼び出します。ビルド モード デバッグ。デバッグせずにアプリを実行すると、正しいテキストが出力されます。
827ccb0eea8a706c4c34a16891f84e7b *test.txt
何かキーを押すと続行します 。. .
デバッグでアプリを実行すると、印刷する前に3秒間待機します
エラー False False False '' ''
これは実際の問題を単純化したものです。このコードは複雑なコードのバックボーンであり、md5sums.exe のデバッグなしでリリース時にハングアップしますが、他のプログラムでは機能します。Coplex コードもvar a = proc.WaitForExit(timeout);でハングします。添付の例のようにタイムアウトするまで。一方、この単純化は、デバッガーなしのリリースで機能します。また、この問題はすべて Windows 10 で始まり、Windows 7 ではすべて正常に機能しました。
[編集] なぜmd5sums.exeが問題を引き起こすのか、また何か他のものを使用した場合は理解できません。FileName = "ping"、Arguments = "localhost" すべてが期待どおりに機能します。
[EDIT2] 複雑なプログラムが Windows 10 で動作を停止しました (リリース - デバッグなしで実行) が、この例は Windows 7 でもハングします (デバッグ - デバッグ付きで実行)
static void TestProcess()
{
using (var proc = new Process())
{
proc.StartInfo.FileName = "md5sums.exe";
proc.StartInfo.WorkingDirectory = @"C:\Temp\ProcessDebug";
proc.StartInfo.Arguments = "-u test.txt";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
proc.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
outputWaitHandle.Set();
else
output.AppendLine(e.Data);
};
proc.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
errorWaitHandle.Set();
else
error.AppendLine(e.Data);
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
var timeout = 1000;
var a = proc.WaitForExit(timeout);
var b = outputWaitHandle.WaitOne(timeout);
var c = errorWaitHandle.WaitOne(timeout);
if (a && b && c)
Console.WriteLine(output.ToString());
else
Console.WriteLine($"Error {a} {b} {c} '{output}' '{error}'");
}
}
}