日期:2014-05-17  浏览次数:20894 次

测试就未响应,应该是进程的问题,求助,怎么解决!
代码如下:
  Process p = new Process();//创建进程对象
            p.StartInfo.FileName = "cmd.exe";//执行CMD程序
            p.StartInfo.UseShellExecute = false;//不调用系统外壳程序启动进程
            p.StartInfo.RedirectStandardInput = true;//不重定向输入
            p.StartInfo.RedirectStandardOutput = true;//不重定向输出
            p.StartInfo.RedirectStandardError = true;//将应用程序错误输入输出流
            p.StartInfo.CreateNoWindow = true;//创建DOS窗口
            p.Start();//开始进程
            p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine("svscmd");//执行DOS命令

            textBox1.Text = "";//
            StreamReader reader = p.StandardOutput;//截取输出流
            string line = reader.ReadLine();//每次读取一行
            while (!reader.EndOfStream)
            {
                textBox1.AppendText(line + "\n");
                line = reader.ReadLine();
            }
            p.StandardInput.WriteLine("exit");
            p.BeginOutputReadLine();
            p.StandardOutput.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程          
            p.Close();//关闭进程
            reader.Close();//关闭流


新手分不多,求帮助!
C#?dos

------解决方案--------------------
我上网查了一下,可能你与这个问题类似,也就是说要在p.StandardInput.WriteLine("svscmd");//执行DOS命令 后面加上p.StandardInput.Close(); 然后才能阻止标准输入流所谓的deadlock或者说是hangs。

参考:http://www.codeproject.com/Questions/74521/StandardOutput-ReadLine-Freezes

Deadlocks are a common issue when working with RedirectStandardInput & Output. The way to avoid this is to close the RedirectI