2013-08-20 120 views
0

我編寫了一個MVC操作,該操作使用輸入參數運行實用程序並將實用程序輸出寫入響應html。這裏是完整的方法:顯示在MVC中運行的命令行進程的進度

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

該實用程序需要大約一分鐘的時間才能完成,並沿途顯示相關信息。 是否可以在用戶的​​瀏覽器上顯示信息?

回答

0

我希望這個鏈接可以幫到您:Asynchronous processing in ASP.Net MVC with Ajax progress bar
您可以調用Controller的操作方法並獲取進程狀態。

enter image description here

控制器代碼:

/// <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"); 
      }; 
     }); 
    } 
+0

由於我用的是JS做類似的事情 – Mortalus