zoukankan      html  css  js  c++  java
  • 异步执行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         }
  • 相关阅读:
    电路的耦合方式
    PCBA与PCB的区别
    vue记住密码功能
    数组变异
    element时间选择器插件转化为YYYY-MM-DD的形式
    box-shadow
    从后台传select的值
    jQuery事件(持续更新中)
    JavaScript对象(持续更新中)
    15分XX秒后订单自动关闭(倒计时)
  • 原文地址:https://www.cnblogs.com/yomho/p/3753512.html
Copyright © 2011-2022 走看看