Call and Close process Method

Close Proces

 

代码
   public static void CloseProcs(string procName)
        {
            Process[] procs 
= Process.GetProcessesByName(procName);
            
foreach (Process proc in procs)
            {
                proc.CloseMainWindow();
                proc.Close();
            }
        }

 

 

Call Process

 

代码
 1      public static bool CallProc(string procName, string args, int timeout)
 2         {
 3             procEM = new StringBuilder();
 4             procOM = new StringBuilder();
 5             Process proc = new Process();
 6             proc.StartInfo.FileName = procName;
 7             proc.StartInfo.WorkingDirectory = Program.workingFolder;
 8             proc.StartInfo.Arguments = args;
 9             proc.StartInfo.UseShellExecute = false;
10             proc.StartInfo.RedirectStandardOutput = true;
11             proc.StartInfo.RedirectStandardError = true;
12             proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived); // using asynchronous read to avoid deadlock conditions
13             proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
14             proc.Start();
15             proc.BeginOutputReadLine();
16             proc.BeginErrorReadLine();
17             if (!proc.WaitForExit(timeout))
18             {
19                 proc.Kill();
20                 procEM.AppendLine("Run " + procName.Substring(procName.LastIndexOf('\\'+ 1+ " time out!\n");
21                 return false;
22             }
23             if (proc.ExitCode != 0)
24             {
25                 return false;
26             }
27             return true;
28         }

 

 

posted on 2010-02-25 16:03  Paul_  阅读(263)  评论(0编辑  收藏  举报

导航