0

プロセスを使用して、process.StartInfo.RedirectStandardOutput = true; の使用中に例外が発生しています。例外は、「StandardOut がリダイレクトされていないか、プロセスがまだ開始されていない」の下にあります。

アプリケーションGUIをハングさせるprocess.WaitForExitを使用しているので、process.StandardOutput.ReadToEnd();を使用しました。MSDN による WaitForExit の前ですが、プロセスが無限に実行され、ログ ファイルで上記の例外が発生し、GUI もハングします。

                Process process = new Process();
                process.StartInfo.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);


                string strArguments = ""; 

                process.StartInfo.FileName = @"appname.bat";
                process.StartInfo.Arguments = strArguments;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;

                process.Start();


                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                process.Close();

どこに欠けているのか、コードのどこにエラーがあるのか​​ わかりません... waitforexitでもタイムアウトを試みましたが、成功しませんでした。

4

2 に答える 2

2

これを試して

 process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
    process.Start();
    process.BeginOutputReadLine();
于 2012-09-14T13:40:45.257 に答える
0

process.WaitForExit(); 文字列出力 = process.StandardOutput.ReadToEnd();

これを試してみてください。コードの順序を変更します。

于 2015-06-12T07:13:33.457 に答える