我正在尝试从 ncftpput.exe 获取输出流并能够异步操作流。ncftpput.exe 不会逐行打印它的流,而是用新信息不断更新同一行(这可能是相关的)。它间歇性地工作 - 有时我会得到信息,有时我不会。
本质上,有没有一种方法可以让我经常刷新它的线路,以便以更安全和更常规的方式重定向?
这是我到目前为止所得到的(我已经对其进行了注释,消除了不相关的无关信息,但这是本质):
class Program
{
static int main()
{
internal Process m_Tool { get; set; }
internal ProcessStartInfo m_StartInfo { get; set; }
internal static string m_StandardData { get; set; }
internal static string m_ErrorData { get; set; }
m_Tool = new Process();
m_StartInfo = new ProcessStartInfo();
m_StartInfo.FileName = @"C:\utils\ncftpput.exe";
m_StartInfo.UseShellExecute = false;
m_StartInfo.RedirectStandardOutput = true;
m_StartInfo.RedirectStandardError = true;
m_StandardData = "";
m_ErrorData = "";
m_StartInfo.Arguments = /* Various starting args */
m_Tool.StartInfo = m_StartInfo;
m_Tool.Start();
string standardLine;
string errorLine;
try
{
m_Tool.OutputDataReceived += ProcessStandardDataHandler;
m_Tool.ErrorDataReceived += ProcessErrorDataHandler;
m_Tool.BeginErrorReadLine();
m_Tool.BeginOutputReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
while (!m_Tool.HasExited)
{
System.Threading.Thread.Sleep(5000);
standardLine = m_StandardData;
errorLine = m_ErrorData;
}
}
private static void ProcessErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
// Add the text to the collected output.
m_ErrorData = outLine.Data.ToString();
m_DataReceived = true;
}
}
private static void ProcessStandardDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
// Add the text to the collected output.
m_StandardData = outLine.Data.ToString();
}
}
}
提前致谢!!