zoukankan      html  css  js  c++  java
  • Process启动.exe,当.exe内部抛出异常时,总会弹出一个错误提示框,阻止Process进入结束

     1 public class TaskProcess
     2     {
     3         [DllImport("kernel32.dll", SetLastError = true)]
     4         public static extern int SetErrorMode(int wMode);
     5 
     6         public Process process { get; set; }
     7 
     8         public void Do()
     9         {
    10             try
    11             {
    12                 int oldMode = SetErrorMode(3);
    13 
    14                 this.process = new Process();
    15                 this.process.EnableRaisingEvents = true;
    16                 this.process.StartInfo.FileName = @"\172.21.11.10FilePlatformExecuteFileLSystem.exe";
    17                 this.process.StartInfo.Arguments = @"8 \172.21.11.10FilePlatformConfigurationAppSettings.config 17 1 0";
    18                 this.process.StartInfo.RedirectStandardError = true;
    19                 this.process.StartInfo.RedirectStandardInput = true;
    20                 this.process.StartInfo.RedirectStandardOutput = true;
    21                 this.process.StartInfo.CreateNoWindow = false;
    22                 this.process.StartInfo.ErrorDialog = false;
    23                 this.process.StartInfo.UseShellExecute = false;
    24                 this.process.Start();
    25 
    26                 SetErrorMode(oldMode);
    27 
    28                 ThreadPool.QueueUserWorkItem(DoWork, process);
    29 
    30                 process.WaitForExit();
    31 
    32                 Console.WriteLine("ExitCode is " + this.process.ExitCode);
    33             }
    34             catch (Exception ex)
    35             {
    36                 Console.WriteLine(ex.Message);
    37                 Console.WriteLine(ex.StackTrace);
    38             }
    39         }
    40 
    41         public ArrayList Out = ArrayList.Synchronized(new ArrayList());
    42 
    43         private void DoWork(object state)
    44         {
    45             Process process = state as Process;
    46 
    47             if (process != null)
    48             {
    49                 try
    50                 {
    51                     string line = process.StandardOutput.ReadLine();
    52 
    53                     do
    54                     {
    55                         Out.Add(line);
    56 
    57                         line = process.StandardOutput.ReadLine();
    58                         Console.WriteLine(line);
    59                     }
    60                     while (line != null);
    61 
    62                     process.StandardInput.WriteLine("exit");
    63                 }
    64                 catch (Exception ex)
    65                 {
    66                     Console.WriteLine(ex.Message);
    67                     Console.WriteLine(ex.StackTrace);
    68                 }
    69             }
    70         }
    71     }

    参考文章:

    http://stackoverflow.com/questions/673036/how-to-handle-a-crash-in-a-process-launched-via-system-diagnostics-process

  • 相关阅读:
    结合<span id="outer"><span id="inter">text</span></span>这段结构,谈谈innerHTML、outerHTML、innerText之间的区别
    字符串的方法slice、substr、substring对比
    为什么两个一样的对象,用===打印是false
    this指向
    复制对象的方法
    Promise以及async和await的用法
    前端性能优化&&网站性能优化
    P1510 精卫填海
    分解质因数
    P2648 赚钱
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/4534128.html
Copyright © 2011-2022 走看看