使用进程时程序不会终止 [英] Program doesn’t terminate when using processes

查看:86
本文介绍了使用进程时程序不会终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ProcessStartInfoProcess,我想启动一个程序(例如getdiff.exe),然后读取该程序产生的所有输出.稍后,我将以一种更具建设性的方式使用数据,现在我只想打印数据以确保其正常工作.但是,程序并未按预期终止.有人会为什么吗?谢谢你.

Using the ProcessStartInfo and Process I would like start a program (e g getdiff.exe) and then read all the output that program produces. Later I will use the data in a more constructive way put right now I just want to print the data to ensure it works. However the program doesn’t terminate as it should. Does anyone se why? Thank you in advanced.

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;                
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";

Process p = Process.Start(psi);
string read = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Console.WriteLine(p);
Console.WriteLine("Complete");

p.Close();

将程序更改为此可以使其正常工作:

Changing the program to this got it working correctly:

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;                
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";

Process p = Process.Start(psi);
StreamReader read = p.StandardOutput;

while (read.Peek() >= 0)
    Console.WriteLine(read.ReadLine());

Console.WriteLine("Complete");
p.WaitForExit();
p.Close();

推荐答案

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;                
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";

Process p = Process.Start(psi);
StreamReader read = p.StandardOutput;

while (read.Peek() >= 0)
    Console.WriteLine(read.ReadLine());

Console.WriteLine("Complete");
p.WaitForExit();
p.Close();

这篇关于使用进程时程序不会终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆