• 异步执行Dos命令


     1 //Minute
     2         const int _defaultTimeOut = 24 * 60;
     3 
     4         /// <summary>
     5         /// 执行命令行
     6         /// </summary>
     7         /// <param name="cmdLine">程序集名称(或批处理文件)</param>
     8         /// <param name="args">参数</param>
     9         /// <param name="outputsAction">处理命令行的输出流信息</param>
    10         /// <param name="errorsAction">处理命令行的错误输出信息</param>
    11         /// <param name="timeOut">超时时间(单位:分钟)</param>
    12         public static void ExecuteCommandeLine(String cmdLine, String args, Action<object, DataReceivedEventArgs> outputsAction, Action<object, DataReceivedEventArgs> errorsAction, int timeOut = _defaultTimeOut)
    13         {
    14             Process process = new Process();
    15             process.StartInfo.RedirectStandardError = true;
    16             process.StartInfo.RedirectStandardOutput = true;
    17             process.StartInfo.RedirectStandardInput = true;
    18             process.StartInfo.CreateNoWindow = true;
    19             process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    20             process.StartInfo.UseShellExecute = false;
    21             process.StartInfo.LoadUserProfile = true;
    22             process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
    23 
    24             if (cmdLine.ToLower().Contains(".bat"))
    25             {
    26                 process.StartInfo.FileName = "cmd.exe";
    27                 process.StartInfo.Arguments = "/c" + " " + cmdLine + " " + args;
    28             }
    29             else
    30             {
    31                 process.StartInfo.FileName = cmdLine;
    32                 process.StartInfo.Arguments = args;
    33             }
    34             if (outputsAction != null)
    35                 process.OutputDataReceived += new DataReceivedEventHandler(outputsAction);
    36             if (errorsAction != null)
    37                 process.ErrorDataReceived += new DataReceivedEventHandler(errorsAction);
    38 
    39             process.Start();
    40             process.BeginOutputReadLine();
    41             process.BeginErrorReadLine();
    42 
    43             int i = 0;
    44             while (!process.HasExited)
    45             {
    46                 if (i++ > timeOut * 60 && !process.HasExited)
    47                 {
    48                     process.Kill();
    49                     process.Close();
    50                     throw new Exception(string.Format("Command line execution timeout.cmdLine:{0},args:{1}", cmdLine, args));
    51                 }
    52                 Thread.Sleep(1000);
    53             }
    54         }
    55 
    56 
    57         /// <summary>
    58         /// 执行命令行
    59         /// </summary>
    60         /// <param name="cmdLine">程序集名称(或批处理文件)</param>
    61         /// <param name="args">参数</param>
    62         /// <param name="outputs">命令行输出信息</param>
    63         /// <param name="errors">命令行输出错误信息</param>
    64         /// <param name="timeOut">超时时间(单位:分钟)</param>
    65         public static void ExecuteCommandeLine(String cmdLine, String args, out string outputs, out string errors, int timeOut = _defaultTimeOut)
    66         {
    67             StringBuilder outputString = new StringBuilder();
    68             StringBuilder errorString = new StringBuilder();
    69             outputs = string.Empty;
    70             errors = string.Empty;
    71 
    72             ExecuteCommandeLine(cmdLine, args,
    73                 (o, d) =>
    74                 {
    75                     if (d.Data != null && outputString.MaxCapacity > outputString.Length + d.Data.Length + 10)
    76                         outputString.AppendLine(d.Data);
    77                 },
    78                 (o, d) =>
    79                 {
    80                     if (d.Data != null && errorString.MaxCapacity > errorString.Length + d.Data.Length + 10)
    81                         errorString.AppendLine(d.Data);
    82                 },
    83                 timeOut);
    84             outputs = outputString.ToString();
    85             errors = errorString.ToString();
    86         }
  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/yomho/p/3753512.html
Copyright © 2020-2023  润新知