从ASP.NET MVC调用控制台应用程序,但不要';我不想等待答复

本文关键字:我不想 等待 MVC NET ASP 调用 控制台 应用程序 | 更新日期: 2023-09-27 18:00:23

我正在努力实现这样的目标

    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尚未重定向或这个过程还没有开始。

描述:未处理的异常在执行当前web请求。请查看堆栈跟踪以获取有关的详细信息错误及其来源代码。

异常详细信息:System.InvalidOperationException:StandardOut尚未重定向或这个过程还没有开始。

来源错误:

第56:p行=Process.GetProcessById(pid);第57行:第58行:返回查看("索引",新内容(){Text=p.StandardOutput.ReadToEnd()});线59:}第60:}行

源文件:Z: ''Junk''MVCtoConsole''MVCtoConsole''Controllers''HomeController.cs线路:58

堆栈跟踪:

[无效操作异常:StandardOut尚未重定向或进程尚未开始。]
System.Diagnostics.Process.get_StandardOutput()+1172937 MVCtoConsole.Controllers.HomeController.Update()在里面Z: ''Junk''MVCtoConsole''MVCtoConsole''Controllers''HomeController.cs:58lambda_ method(ExecutionScope,ControllerBase,Object[])+40
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext控制器上下文,IDictionary 2 parameters) +178
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2参数)+24
System.Web.Mvc.<>c_DisplayClassd.b_a()+52 System.Web.Mvc.ControllerActionInvoker.IInvokeActionMethodFilter(IActionFilter筛选器,ActionExecutingContextpreContext、Func CCD_,ActionDescriptor ActionDescriptor,IDictionary 2 parameters) +192
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314 System.Web.Mvc.Controller.ExecuteCore() +105 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
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.Anc.WappedAsyncResult`1.End()+59 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResultasyncResult)+44
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)+7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8690318 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔&同步完成)+155

版本信息:Microsoft.NET框架版本: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);
            }
        }
    }
}

从ASP.NET MVC调用控制台应用程序,但不要';我不想等待答复

您还需要设置

p.UseShellExecute=错误

抱歉没有看到你已经设置了

您应该在请求输出之前检查p状态,此时可能已经结束