Okay, thanks for your response, here is a short test app I wrote that shows
the same behavior as the app I'm trying to write. The process I'm trying to
run is pscp.exe which can be downloaded from here:
http://www.chiark.greenend.org.uk/~s.../download.html
(NB: If anybody knows a better way to do scp to a unix box, I'd be
interested to here about it.)
using System;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace ProcessTest
{
class Class1
{
public string m_ProcessArgs;
public string m_ProcessApp;
public string m_Password;
public string m_UserID;
public string m_Address;
public string m_CalcPath;
public Process m_Running;
public StreamReader m_StdOut;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 myClass = new Class1();
myClass.m_UserID = "*******";
myClass.m_Address = "********"; // address of server
myClass.m_CalcPath = "/*********/"; // path on server
myClass.m_Password = "******";
myClass.PutFiles(@"C:\Data");
// ******* removed for security reasons!!
Console.ReadLine();
}
public void PutFiles(string FilePath)
{
m_ProcessArgs = "-r -pw " + m_Password + " " + FilePath + " " + m_UserID +
"@" + m_Address + ":" + m_CalcPath;
m_ProcessApp = "pscp.exe";
Console.WriteLine("Starting transfer thread");
Thread myThread = new Thread(new ThreadStart(TransferProcess));
myThread.Start();
Console.WriteLine("The transfer thread has been started");
}
private void TransferProcess()
{
ProcessStartInfo pInfo = new ProcessStartInfo(m_ProcessApp,m_ProcessArgs);
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.CreateNoWindow = true;
m_Running = new Process();
m_Running.StartInfo = pInfo;
Console.WriteLine("TransferProcess is starting process....");
m_Running.Start();
// This line appears immediately
Console.WriteLine("The Process has started");
m_StdOut = m_Running.StandardOutput;
// This line doesn't appear until the process has completed
Console.WriteLine("This was read from the StdOut: " + m_StdOut.Read());
m_Running.WaitForExit();
// This appears immediately after the last line, indicating that the last
line wasn't displayed
// until the end of the process
Console.WriteLine("File Transfer Done");
Console.ReadLine();
}
}
}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Matt Burland <wjousts@[nospam]hotmail.com> wrote: I'm having a problem with redirecting the StandardOutput of a process
that I use to run a DOS program in my application. The problem is that I when I
start my process (which I do in a separate thread) I can't read anything
from the stdout before the process actually finishes. If I do something
like this:
Process myProcess = new Process(myStartInfo)
// this gets shown straight away
MessageBox.Show("Process Started");
string s = myProcess.StandardOutput.Readline();
// this doesn't get shown until the process has finished
MessageBox.Show(s);
myProcess.WaitForExit();
// this gets shown immediately following the last MessageBox
MessageBox.Show("Process Finished");
It's clear that I'm not reading anything from the stdout before the
process has finished. This is a problem because the program I am running
transfers files to a server and can take a long time to finish, but I'm unable to
give the user any information about what is going on during the transfer.
Can somebody please tell me how to read the stdout without hanging up
things up until the process has finished? I can't believe there isn't a way to
do this!!
I haven't seen this problem - is the standard output (not standard
error) definitely writing whole lines of text? Could you give a short
but complete example? (I remember writing a small "echo" program a
while ago which showed standard input and standard output working, for
example.)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too