【发布时间】:2015-07-28 20:47:23
【问题描述】:
我需要调用外部程序将电子邮件下载到文件夹中。这个外部程序是用 Excelsior JET (Jar to Exe) 编译的Java。
我是这样称呼的:
var process = new Process
{
StartInfo =
{
Arguments = Arguments(folder),
FileName = Path.Combine(@"C:/Fakepath"), "GetMail.exe"),
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
}
};
process.Start();
process.WaitForExit();
var output = process.StandardOutput.ReadToEnd();
var outputError = process.StandardError.ReadToEnd();
//if (!String.IsNullOrEmpty(output))
// LogWriter.Log("GetMail", output.Trim());
if (!String.IsNullOrEmpty(outputError))
LogWriter.Log("GetMail Erro", outputError.Trim());
process.Close();
我猜它的输出有问题,比如this other thread here。
Java 应用程序会在不应该的地方停止,例如在两次 Log 调用之间:
try
{
for (Address addr : msg.getFrom())
{
AppConfig.getLogControl().WriteLog("\tFrom.............:" + addr);
}
} catch (Exception e) { }
MimeMessage msgMIME = null;
try
{
AppConfig.getLogControl().WriteLog("\tTesting message type... ");
msg.isMimeType("text/*");
AppConfig.getLogControl().WriteLog("\t\tMultipart type");
} catch (MessagingException ex)
{
AppConfig.getLogControl().WriteLog("\t\tMIME type");
AppConfig.getLogControl().WriteLog("\tDownloading data... ");
msgMIME = TrataMimeMessage(msg);
}
它打印“From....”并停止,如果我终止 C# 进程,Java 应用程序将继续打印其余部分。
编辑
reading this page 之后,我想我知道我做错了什么。 MSDN 页面的部分之一:
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
【问题讨论】:
标签: c# process redirectstandardoutput