【问题标题】:Display progress of a command line process that is running in MVC显示在 MVC 中运行的命令行进程的进度
【发布时间】:2013-08-20 06:17:16
【问题描述】:

我编写了一个 MVC 操作,它使用输入参数运行实用程序并将实用程序输出写入响应 html。这是完整的方法:

        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();

该实用程序大约需要一分钟才能完成,并在此过程中显示相关信息。 是否可以在用户浏览器上运行时显示信息?

【问题讨论】:

    标签: asp.net-mvc command-line system.diagnostics


    【解决方案1】:

    我希望这个链接会有所帮助:Asynchronous processing in ASP.Net MVC with Ajax progress bar
    可以调用Controller的action方法,获取进程的状态。

    控制器代码:

        /// <summary>
        /// Starts the long running process.
        /// </summary>
        /// <param name="id">The id.</param>
        public void StartLongRunningProcess(string id)
        {
            longRunningClass.Add(id);            
            ProcessTask processTask = new ProcessTask(longRunningClass.ProcessLongRunningAction);
            processTask.BeginInvoke(id, new AsyncCallback(EndLongRunningProcess), processTask);
        }
    

    jQuery 代码:

        $(document).ready(function(event) {
            $('#startProcess').click(function() {
                $.post("Home/StartLongRunningProcess", { id: uniqueId }, function() {
                    $('#statusBorder').show();
                    getStatus();
                });
                event.preventDefault;
            });
        });
    
        function getStatus() {
            var url = 'Home/GetCurrentProgress/' + uniqueId;
            $.get(url, function(data) {
                if (data != "100") {
                    $('#status').html(data);
                    $('#statusFill').width(data);
                    window.setTimeout("getStatus()", 100);
                }
                else {
                    $('#status').html("Done");
                    $('#statusBorder').hide();
                    alert("The Long process has finished");
                };
            });
        }
    

    【讨论】:

    • 感谢我使用 JS 做类似的事情
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多