Programming2010. 11. 19. 09:27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ProcessMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            killProcess();
        }

        private static void killProcess()
        {
            Console.WriteLine("Monitoring target processes");
            int intInterval = 1000; // 1 second
            string[] procName = new string[] { "PCEncure.apo", "TMaker" };

            while (true)
            {

                Process[] runningNow = Process.GetProcesses();
                int i = 0;
                while (i < procName.Length)
                {
                    foreach (Process process in runningNow)
                    {
                        using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
                        {
                            if (process.ProcessName == procName[i])
                            {
                                pcProcess.NextValue();
                                System.Threading.Thread.Sleep(1000);
                                Console.WriteLine("Process:{0} CPU% {1}", process.ProcessName, pcProcess.NextValue());

                                FileStream file = new FileStream("aa.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

                                StreamWriter sw = new StreamWriter(file);

                                if (pcProcess.NextValue() > float.Parse("10"))
                                {
                                    sw.WriteLine(string.Format("\n{0} using over {1}% at {2}", procName[i], pcProcess.NextValue().ToString(), DateTime.Now.ToString()));
                                }
                                sw.Close();
                            }
                        }
                    }
                    i++;
                }
                // Sleep till the next loop
                Thread.Sleep(intInterval);
            }
        }
    }
}

Posted by Mocker