Mega Code Archive

 
Categories / C# / Development Class
 

Start and kill process (2)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; class CommonUtility {     public static void StartProcess(string CommandName)     {         Process p = new Process();         p.StartInfo.FileName = CommandName;         //p.StartInfo.RedirectStandardOutput = true;         //p.StartInfo.UseShellExecute = true;         p.Start();     }     public static void KillProcess(string processname)     {         Process[] pss = Process.GetProcesses();         for (int i = 0; i < pss.Length; i++)         {             if (pss[i].ProcessName.ToLower().Trim() == processname)             {                 pss[i].CloseMainWindow();             }         }         //while (true)         {             System.Threading.Thread.Sleep(1000);             Process[] pss2 = Process.GetProcesses();             for (int i = 0; i < pss2.Length; i++)             {                 if (pss2[i].ProcessName.ToLower().Trim() == processname)                 {                     pss2[i].Kill();                 }             }         }     } }