Я пытаюсь добиться чего-то подобного.
private Process p;
//
// GET: /Home/
[HttpGet]
public ActionResult Index()
{
return View(new Contents() { Text = "Hello" });
}
[HttpPost]
public ActionResult Processing()
{
// Get the file path of your Application (exe)
string filePath = @"Z:\Junk\MVCtoConsole\Sample Console App\bin\Debug\Sample Console App.exe";
ProcessStartInfo info = new ProcessStartInfo(filePath);
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
p = Process.Start(info);
p.WaitForExit(1);
Session["pid"] = p.Id;
return View("Index", new Contents() { Text = "Processing" });
}
[HttpPost]
public ActionResult Kill()
{
int pid = (int)Session["pid"];
p = Process.GetProcessById(pid);
p.Kill();
return View("Index", new Contents() { Text = "Killed" });
}
public ActionResult Update()
{
int pid = (int)Session["pid"];
p = Process.GetProcessById(pid);
return View("Index", new Contents() { Text = p.StandardOutput.ReadToEnd() });
}
Но я получаю следующие ошибки при вызове представления обновления ...
Ошибка сервера в приложении '/'.
StandardOut не был перенаправлен или процесс еще не запущен.
Описание: во время выполнения возникло необработанное исключениетекущий веб-запрос.Пожалуйста, просмотрите трассировку стека для получения дополнительной информации об ошибке и о том, где она возникла в коде.
Сведения об исключении: System.InvalidOperationException: StandardOut не был перенаправлен или процесс еще не запущен.
Ошибка источника:
Строка 56: p = Process.GetProcessById(PID);Строка 57: Строка 58: возвращаемое представление («Индекс», new Contents () {Text = p.StandardOutput.ReadToEnd ()});Строка 59:} Строка 60:}
Исходный файл: Z: \ Junk \ MVCtoConsole \ MVCtoConsole \ Controllers \ HomeController.cs Строка: 58
Трассировка стека:
[InvalidOperationException: StandardOut не было перенаправлено или процесс еще не начался.]
System.Diagnostics.Process.get_StandardOutput () +1172937 MVCtoConsole.Controllers.HomeController.Update () в Z: \ Junk \ MVCtoConsole \ MVCtoControllers \ HomeController.cs: 58 lambda_method (ExecutionScope, ControllerBase, Object []) + 40
System.Web.Mvc.ActionMethodDispatcher.Execute (контроллер ControllerBase, параметры Object []) + 17
System.Web.Mvc.ReflectedActionDescriptor.Execute (ControllerContext controllerContext, IDictionary 2
parameters) +178<br>
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor
actionDescriptor, IDictionary
2 параметра) + 24
System.Web.Mvc. <> C_ DisplayClassd.b _a () +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (фильтр IActionFilter, preContext ActionExecutingContext, фильтры Func 1 continuation) +254
System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c()
+19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList
1, actionDescriptor actionDescriptor, IDictionary 2 parameters) +192<br>
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName)
+314 System.Web.Mvc.Controller.ExecuteCore()
+105 System.Web.Mvc.ControllerBase.Execute(RequestContext
requestContext) +39<br>
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext
requestContext) +7<br>
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4()
+34 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
+21 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7 (IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End () +59 System.Web.Mvc.MvcHandler.EndProcessRequest (IAsyncResult asyncResult) + 44
System.Web.Mvc.MvcHandler.Web.IHttpAsyncHandler.EndProcessRequest (результат IAsyncResult) + 7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +8690318 System.Web.HttpSecE1038 *
Информация о версии: Microsoft .NET Framework Версия: 2.0.50727.5446;ASP.NET версия: 2.0.50727.5420
Есть идеи о том, как этого можно достичь?
О, мое консольное приложение не работаеточень много сейчас, так как я просто пытаюсь выяснить, сработает ли это.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Sample_Console_App
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Triggered");
for (int i = 0; i < 100000; i = i + 100)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
}
}