读书人

测试就未响应应该是进程的有关问题

发布时间: 2013-11-01 14:43:02 作者: rapoo

测试就未响应,应该是进程的问题,求助,怎么解决!
代码如下:

  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;//创—OS窗口
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 RedirectIO. In your example you already flushed it but did not close it.

读书人网 >C#

热点推荐