I wrote an MVC action that runs a utility with input parameters and writes the utilities output to the response html. here is the full method:
var jobID = Guid.NewGuid();
// save the file to disk so the CMD line util can access it
var inputfilePath = Path.Combine(@"c:\", String.Format("input_{0:n}.json", jobID));
var outputfilePath = Path.Combine(@"c:\", String.Format("output{0:n}.json", jobID));
using (var inputFile = System.IO.File.CreateText(inputfilePath))
{
inputFile.Write(i_JsonInput);
}
var psi = new ProcessStartInfo(@"C:\Code\FoxConcept\FoxConcept\test.cmd", String.Format("{0} {1}", inputfilePath, outputfilePath))
{
WorkingDirectory = Environment.CurrentDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (var process = new Process { StartInfo = psi })
{
// delegate for writing the process output to the response output
Action<Object, DataReceivedEventArgs> dataReceived = ((sender, e) =>
{
if (e.Data != null) // sometimes a random event is received with null data, not sure why - I prefer to leave it out
{
Response.Write(e.Data);
Response.Write(Environment.NewLine);
Response.Flush();
}
});
process.OutputDataReceived += new DataReceivedEventHandler(dataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(dataReceived);
// use text/plain so line breaks and any other whitespace formatting is preserved
Response.ContentType = "text/plain";
// start the process and start reading the standard and error outputs
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
// wait for the process to exit
process.WaitForExit();
// an exit code other than 0 generally means an error
if (process.ExitCode != 0)
{
Response.StatusCode = 500;
}
}
Response.End();
The utility takes around a minute to complete and it displays relevant information along the way. is it possible to display the the information as it runs on the user's browser ?