dimanche 31 août 2014

how to stream output as it happens C#


Vote count:

0




I got this code from microsoft support site and it allows you to run a external process from your application it give output after the execution of program but i would like to stream output as it happens on screen how do i do it?



using System;
using System.Diagnostics;
using System.IO;

namespace Way_Back_Downloader
{

internal class RunWget
{
internal static string Run(string exeName, string argsLine, int timeoutSeconds)
{
StreamReader outputStream = StreamReader.Null;
string output = "";
bool success = false;

try
{
Process newProcess = new Process();
newProcess.StartInfo.FileName = exeName;
newProcess.StartInfo.Arguments = argsLine;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.CreateNoWindow = true;
newProcess.StartInfo.RedirectStandardOutput = true;
newProcess.Start();



if (0 == timeoutSeconds)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();

newProcess.WaitForExit();
}
else
{
success = newProcess.WaitForExit(timeoutSeconds * 1000);

if (success)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
}

else
{
output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
}

}
}
catch (Exception exception)
{
throw (new Exception("An error occurred running " + exeName + ".", exception));
}
finally
{
outputStream.Close();
}
return "\t" + output;
}
}
}


asked 9 secs ago







how to stream output as it happens C#

Aucun commentaire:

Enregistrer un commentaire