Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
}


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.
Posted

1 solution

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();
}


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

Good luck!
 
Share this answer
 
Comments
Tony Richards 21-Sep-10 8:42am    
You really don't want to use Application.DoEvents if you can in any way avoid it. It is much better to use a Thread, ThreadPool or BackgroundWorker. DoEvents can cause some seriously strange behaviour if you don't handle it properly (e.g. events re-entering that don't support re-entry).
E.F. Nijboer 21-Sep-10 10:06am    
Absolutely true, but this is simply the easiest way to test this simply by adding a minimal of code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900