.net - 从 Dot Net MVC 4 异步执行批处理脚本

标签 .net asp.net-mvc-4 console-application async-await

我有一个 dot net MVC 4 应用程序,其中用户使用“创建” View 创建请求,并根据 View 中的参数, Controller 应异步执行 Windows 批处理脚本。

我使用以下代码来运行批处理脚本:

Process process = new Process();

ProcessStartInfo ProcessInfo;
ProcessInfo = new ProcessStartInfo("cmd", "/c  batchfile");
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
ProcessInfo.RedirectStandardError = true;
ProcessInfo.RedirectStandardOutput = true;
ProcessInfo.Arguments = string.Format("{0} {1} {2}", arg1, arg2, arg3); ;

process = Process.Start(ProcessInfo);
process.WaitForExit();

控制台应用程序进程已启动,但它总是无休止地挂起/等待。标准输出日志显示:

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Program Files (x86)\IIS Express>

只有当我手动终止该进程时,该进程才会退出。我尝试运行 Visual Studio 管理员模式,但没有帮助。

有人可以建议我如何解决这个问题吗?

最佳答案

这是一个reference article关于传递给 cmd.exe 的命令行参数,我建议您检查。

然后尝试这样:

var psi = new ProcessStartInfo("cmd.exe")
{
    Arguments = string.Format("/k \"\"batchfile.bat\" \"{0}\" \"{1}\" \"{2}\"\"", arg1, arg2, arg3),
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardOutput = true
};

using (var process = Process.Start(psi))
{
    process.WaitForExit();
}

关于.net - 从 Dot Net MVC 4 异步执行批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259868/

相关文章:

c# - 如何以编程方式检测 Windows 桌面应用程序中是否启用/禁用了 javascript? (网络浏览器控件)

c# - Action <T> 用作参数

c# - 如何返回 IQueryable<T> 以供进一步查询

c# - 在另一个管理员向 channel 发送帖子后, channel 管理员 Telegram 机器人停止获取更新

tomcat - solr commit 返回 400 错误请求,在日志 :- Unknown commit parameter 'waitFlush' 中

c# - xsl -fo 转pdf

c# - 通用集合继承

asp.net-mvc-4 - MVC 4.0 WebApi 应用程序和混合身份验证

c# - 用于获取刷新 token 的 Google C# 客户端库

c# - 如何强制控制台只接受一定范围内的数字?