从psftp.exe异步捕获标准输出 [英] capturing standard output ASYNCHRONOUSLY from psftp.exe

查看:119
本文介绍了从psftp.exe异步捕获标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#/.net 2.0 winform应用程序内部调用psftp.exe:

I''m making a call to psftp.exe from inside my C#/.net 2.0 winform app:

 private void FtpUpload()
{    
      Process psftp = new Process();
      psftp.StartInfo.FileName = "psftp.exe";

      psftp.StartInfo.Arguments = "XXX.XXX.XXX.XXX -l username -pw password -be -b " + batchFileName +" -batch";
      psftp.StartInfo.UseShellExecute = false;
      psftp.StartInfo.CreateNoWindow = true;
      psftp.StartInfo.RedirectStandardOutput = true;
      psftp.StartInfo.RedirectStandardError = true;
      psftp.OutputDataReceived += new DataReceivedEventHandler(psftp_OutputDataReceived);

      psftp.ErrorDataReceived += new DataReceivedEventHandler(psftp_ErrorDataReceived);

      psftp.Exited += new EventHandler(psftp_Exited);
      psftp.EnableRaisingEvents = true;
      psftp.Start();

      psftp.BeginOutputReadLine();
      psftp.BeginErrorReadLine();
      psftp.WaitForExit();
}
void psftp_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    DoSomething();
}



仅在处理的结束处调用事件处理程序.
请注意,我也尝试了而不是 psftp.WaitForExit()行.

谢谢.



The event handler is called only at the end of processing.
Please note that I tried this without the psftp.WaitForExit() line as well.

Thank you.

推荐答案

似乎您是在主线程中进行操作,因此阻塞了应用程序的消息循环.这也意味着如果操作花费很长时间,您将收到消息,表明程序不再响应.一种解决方案是实现某些线程或仅处理如下事件:

替换为:
It looks like you are doing the operation from within the main thread and therefore blocking the application messageloop. This would also mean that if the operation takes to long, you would get the message that the program isn''t responding anymore. A solution is to implement some threading or simply process the events like this:

replace this:
psftp.WaitForExit();



像这样:



with something like this:

while (!psftp.WaitForExit(100))
{
  Application.DoEvents();
}



现在每100毫秒处理一次事件,这意味着可以执行调用的事件.

祝你好运!



now every 100 milliseconds the events are handled which means that the invoked event can be executed.

Good luck!


这篇关于从psftp.exe异步捕获标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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