Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming Standard Output of a console application to an ASP.NET MVC View
    primarykey
    data
    text
    <p>What I am looking to do is:</p> <p>1) From an MVC View, Start a long running Process. In my case, this process is a seperate Console Application being executed. The Console Application runs for potentially 30 minutes and regurlarily Console.Write's its current actions.</p> <p>2) Back on the MVC View, periodically poll the server to retrieve the latest Standard Out which I have redirected to a Stream (or anywhere I can get access to it for that matter). I'll append newly retieved standard output to a log textbox or something equivalent.</p> <p>Sounds relativly easy. My client side programming is a bit rusty though and I'm having issues with the actual streaming. I would assume this is not an uncommon task. Anyone got a decent solution for it in ASP.NET MVC?</p> <p>Biggest issue seems to be that I cant get the StandardOutput until the end of execution, but I was able to get it with an event handler. Of course, using the event handler seems to lose focus of my output.</p> <p>This is what I was working with so far... </p> <pre><code> public ActionResult ProcessImport() { // Get the file path of your Application (exe) var importApplicationFilePath = ConfigurationManager.AppSettings["ImportApplicationFilePath"]; var info = new ProcessStartInfo { FileName = importApplicationFilePath, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false }; _process = Process.Start(info); _process.BeginOutputReadLine(); _process.OutputDataReceived += new DataReceivedEventHandler(_process_OutputDataReceived); _process.WaitForExit(1); Session["pid"] = _process.Id; return Json(new { success = true }, JsonRequestBehavior.AllowGet); } void _process_OutputDataReceived(object sender, DataReceivedEventArgs e) { _importStandardOutputBuilder.Insert(0, e.Data); } public ActionResult Update() { //var pid = (int)Session["pid"]; //_process = Process.GetProcessById(pid); var newOutput = _importStandardOutputBuilder.ToString(); _importStandardOutputBuilder.Clear(); //return View("Index", new { Text = _process.StandardOutput.ReadToEnd() }); return Json(new { output = newOutput }, "text/html"); } </code></pre> <p>I haven't written the client code yet as I am just hitting the URL to test the Actions, but I'm also interested how you would approach polling for this text. If you could provide the actual code for this too, it would be great. I would assume you'd have a js loop running after kicking off the process that would use ajax calls to the server which returns JSON results... but again, its not my forte so would love to see how its done.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload