显示正在运行的cmd进程的输出



我有一个使用命令提示符调用python脚本的c#代码。该脚本目前运行大约需要25秒。当在命令提示符下运行时,python脚本有几个输出,直到最后输出"done"。我的c#代码运行脚本,但如果我使用"WaitForExit",则永远不会关闭命令提示符。所以我的想法是我需要某种逻辑来检查是否"完成"已经输出,但是互联网在方法论上并不是很有帮助。

这是我的,它目前只输出第一行,"Microsoft Windows [Version 6.1.7601]"。显然,不行。

var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:Optimization";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;

p.Start();

p.StandardInput.WriteLine(@"ipy spot_for_labview.py");
    StreamReader mySR = p.StandardOutput;
    string mystr = mySR.ReadLine();
    Debug.WriteLine(mystr);

p.WaitForExit(25000);  //25000 is a placeholder until better method found.
p.Close();

如果有任何方式关闭进程后,它完成,或得到所有的cmd输出,我洗耳恭听。

您尝试了这个事件吗?的过程。OutputDataReceived事件

或过程。ErrorDataReceived事件

下面是来自MSDN

的代码
    Process sortProcess;
    sortProcess = new Process();
    sortProcess.StartInfo.FileName = "Sort.exe";
    sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

…….…

private static void SortOutputHandler(object sendingProcess, 
            DataReceivedEventArgs outLine)
        {
}

最新更新