// ************************************************************
// This program was written by Mike Gold of Microgold Software Inc.
// Copyright January 17, 2000. This code may be used freely for  
// purpose in aiding in C# design or learning the C# language.
// ************************************************************
namespace ProTracker
{
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
	using System.IO;
	using System.Diagnostics;

    /// <summary>
    ///    Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Button btnRefresh;
		private System.Windows.Forms.Button btnKill;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private Process[] procList = new Process[100];

				
        public Form1()
        {
            InitializeComponent();
			InitializeListView();
			AddProcesses();
		}

        /// <summary>
        ///    Clean up any resources being used.
        /// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

        /// <summary>
        ///    Required method for Designer support - do not modify
        ///    the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
		{
			this.btnRefresh = new System.Windows.Forms.Button();
			this.btnKill = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.SuspendLayout();
			// 
			// btnRefresh
			// 
			this.btnRefresh.Location = new System.Drawing.Point(133, 305);
			this.btnRefresh.Name = "btnRefresh";
			this.btnRefresh.Size = new System.Drawing.Size(103, 28);
			this.btnRefresh.TabIndex = 10;
			this.btnRefresh.Text = "刷新(&R)";
			this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
			// 
			// btnKill
			// 
			this.btnKill.Location = new System.Drawing.Point(10, 305);
			this.btnKill.Name = "btnKill";
			this.btnKill.Size = new System.Drawing.Size(103, 28);
			this.btnKill.TabIndex = 9;
			this.btnKill.Text = "结束进程(&E)";
			this.btnKill.Click += new System.EventHandler(this.btnKill_Click);
			// 
			// listView1
			// 
			this.listView1.Name = "listView1";
			this.listView1.TabIndex = 11;
			// 
			// Form1
			// 
			this.AcceptButton = this.btnRefresh;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(634, 343);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnRefresh,
																		  this.btnKill,
																		  this.listView1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.KeyPreview = true;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "ProTracker 1.0";
			this.ResumeLayout(false);

		}

		protected void btnRefresh_Click (object sender, System.EventArgs e)
		{
			listView1.Clear();
			InitializeListView(); 
			AddProcesses();

		}

		protected void btnKill_Click (object sender, System.EventArgs e)
		{
			if (listView1.SelectedItems.Count < 0 ) 
			{
				MessageBox.Show("Invalid Selection");
				return;
			}
			for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
			{
				ListViewItem li = listView1.SelectedItems[i];
					
				for (int j =0; j<procList.Length; j++)
				{
					if ( procList[j].ProcessName == li.Text  )
					{
						procList[j].Kill();
						
					}
			
				}
				
				listView1.Items.Remove(li);
			}
		}

		protected void AddProcesses ()
		{
			int iVirtual, iPhysical;
			
			for ( int i=0; i<procList.Length; i++)
			{
				iPhysical = procList[i].WorkingSet / 1024;
				iVirtual= procList[i].VirtualMemorySize / 1024;
				listView1.Items.Add( new ListViewItem( new string[]{procList[i].ProcessName,procList[i].Id.ToString(), procList[i].TotalProcessorTime.ToString(), procList[i].BasePriority.ToString(), iPhysical.ToString() + " K",   iVirtual.ToString()+ " K" } ));			
			}
		}

		
		public void InitializeListView()
		{
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2,
																						this.columnHeader3,
																						this.columnHeader4,
																						this.columnHeader5,
																						this.columnHeader6});
			this.listView1.ForeColor = System.Drawing.SystemColors.WindowText;
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(10, 19);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(615, 277);
			this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listView1.TabIndex = 0;
			this.listView1.TabStop = false;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Listview1_KeyDown);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "映象名称";
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "PID";
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "CPU";
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "优先级";
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "内存使用";
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "虚拟内存";
			try
			{
				procList = Process.GetProcesses();
			}
			catch (Exception e){
				MessageBox.Show(e.Message);
				}
		}


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main(string[] args) 
        {
            Application.Run(new Form1());
        }

		
    }
}
