//Minute
const int _defaultTimeOut = 24 * 60; /// <summary>
/// 执行命令行
/// </summary>
/// <param name="cmdLine">程序集名称(或批处理文件)</param>
/// <param name="args">参数</param>
/// <param name="outputsAction">处理命令行的输出流信息</param>
/// <param name="errorsAction">处理命令行的错误输出信息</param>
/// <param name="timeOut">超时时间(单位:分钟)</param>
public static void ExecuteCommandeLine(String cmdLine, String args, Action<object, DataReceivedEventArgs> outputsAction, Action<object, DataReceivedEventArgs> errorsAction, int timeOut = _defaultTimeOut)
{
Process process = new Process();
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.LoadUserProfile = true;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8; if (cmdLine.ToLower().Contains(".bat"))
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c" + " " + cmdLine + " " + args;
}
else
{
process.StartInfo.FileName = cmdLine;
process.StartInfo.Arguments = args;
}
if (outputsAction != null)
process.OutputDataReceived += new DataReceivedEventHandler(outputsAction);
if (errorsAction != null)
process.ErrorDataReceived += new DataReceivedEventHandler(errorsAction); process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine(); int i = 0;
while (!process.HasExited)
{
if (i++ > timeOut * 60 && !process.HasExited)
{
process.Kill();
process.Close();
throw new Exception(string.Format("Command line execution timeout.cmdLine:{0},args:{1}", cmdLine, args));
}
Thread.Sleep(1000);
}
} /// <summary>
/// 执行命令行
/// </summary>
/// <param name="cmdLine">程序集名称(或批处理文件)</param>
/// <param name="args">参数</param>
/// <param name="outputs">命令行输出信息</param>
/// <param name="errors">命令行输出错误信息</param>
/// <param name="timeOut">超时时间(单位:分钟)</param>
public static void ExecuteCommandeLine(String cmdLine, String args, out string outputs, out string errors, int timeOut = _defaultTimeOut)
{
StringBuilder outputString = new StringBuilder();
StringBuilder errorString = new StringBuilder();
outputs = string.Empty;
errors = string.Empty; ExecuteCommandeLine(cmdLine, args,
(o, d) =>
{
if (d.Data != null && outputString.MaxCapacity > outputString.Length + d.Data.Length + 10)
outputString.AppendLine(d.Data);
},
(o, d) =>
{
if (d.Data != null && errorString.MaxCapacity > errorString.Length + d.Data.Length + 10)
errorString.AppendLine(d.Data);
},
timeOut);
outputs = outputString.ToString();
errors = errorString.ToString();
}

异步执行Dos命令的更多相关文章

  1. c#执行Dos命令

    一个执行Dos命令的窗口程序,与各位分享.   效果图:     具体实现在代码中有详细的注释,请看代码.   实现执行CMD命令的核心代码(Cmd.cs):   [csharp]   using S ...

  2. 一个执行Dos命令的窗口程序,与各位分享。

    一个执行Dos命令的窗口程序,与各位分享.   效果图:     具体实现在代码中有详细的注释,请看代码.   实现执行CMD命令的核心代码(Cmd.cs):   [csharp]   using S ...

  3. 【转载】DOS 系统和 Windows 系统有什么关系?为什么windows系统下可以执行dos命令?

    作者:bombless 因为不同的系统都叫 Windows ,这些系统在界面上也有一定连续性并且因此可能造成误解,所以有必要稍微梳理一下几个不同的 Windows 系统.首先是 DOS 上的一个图形界 ...

  4. C#执行外部程序之执行DOS命令和批处理

    在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便.今天就来说一下C#中如何调用外部程序,执行一些特殊任务. 这 ...

  5. C#执行DOS命令(CMD命令)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果.         //dosCommand Dos命令语句         publ ...

  6. 在.net中悄悄执行dos命令,并获取执行的结果(转)

    一.怎样使dos命令悄悄执行,而不弹出控制台窗口? 1.需要执行带“/C”参数的“cmd.exe”命令,它表示执行完命令后立即退出控制台.2.设置startInfo.UseShellExecute = ...

  7. C# 执行DOS命令和批处理

    在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便.今天就来说一下C#中如何调用外部程序,执行一些特殊任务. 这 ...

  8. Python3执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件

    #执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件#这个功能很有用listing=os.popen('ipconfig').readlines()for i in listing: pri ...

  9. C#中执行Dos命令

    //dosCommand Dos命令语句 public string Execute(string dosCommand) { ); } /// <summary> /// 执行DOS命令 ...

随机推荐

  1. centos5.8本地安装yum资源,安装软件包

    首先 [root@mode media]# cd /etc/yum.repos.d/ [root@mode yum.repos.d]# ll total 16 -rw-r--r-- 1 root ro ...

  2. OCP-1Z0-051-名称解析-文章32称号

    32. Which CREATE TABLE statement is valid? A. CREATE TABLE ord_details          (ord_no NUMBER(2) PR ...

  3. 电商指尖---(9).net发展Solr中间Facet特征

    上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计.这一节我们看看如何使用.NET开发Solr中的Facet功能.在讲Facet功能的同一时候, 我们看下.Net中如何使用Sol ...

  4. 在Magento产品页面的使用jqZoom

    Magento在产品页面提供了一个简单的图片放大功能,不是非常好,假设考虑使用放大镜来使用户查看产品的大图.能够考虑使用基于jQuery的插件,jqZoom便是一款优秀的放大镜插件.以下将介绍如何把j ...

  5. 左右lcm,gcd一些性质

    两个整数a,b  他们的最大公约数为n  最小公倍数为m  则有 a,b都能分解为有限个素数的积               12 = 2^2 * 3^1 * 5^0 , 30 = 2^1 * 3^1 ...

  6. 一步一步写算法(之prim算法 中)

    原文:一步一步写算法(之prim算法 中) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] C)编写最小生成树,涉及创建.挑选和添加过程 MI ...

  7. .net4.5的弱事件

    .net4.5的弱事件 没有伟大的愿望,就没有伟大的天才--Aaronyang的博客(www.ayjs.net)-www.8mi.me 1. 事件-我的讲法 老师常告诉我,事件是特殊的委托,为委托提供 ...

  8. 《C语言及程序设计初步》网络课程主页

    题记 CSDN要开在线教育频道,向我发出邀请,看能否开些课程. 我近日一直在关注着翻转课堂,试图在传统课堂中引入新的元素,这须要资源建设的积累.没有时间表的工作,非常难把握. 为CSDN做在线课程,为 ...

  9. Rich IntelliSense for jQuery

    A while back we updated VS2008 IntelliSense to not fail when referencing jQuery.  However, getting I ...

  10. css优先级汇总

    原文:css优先级汇总 我所理解的css优先级:当两个或者多个样式作用于同一个元素时,就会出现css优先级的问题. 多重样式优先级:当内联样式.内部样式和外部样式作用于同一个元素时,属于多重样式的范畴 ...