Thursday, April 7, 2011

How to make task manager in c#. Net.


You can use the System.Diagnostics.Process classes for getting the information about processes running in the computer system. To start new process you can use System.Diagnostics.Process.Start(ProcessnameOrFilename); and to end a process you can Process.Kill()  use  function.

 Codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//to start new process
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            try
            {
                                           System.Diagnostics.Process.Start(openFileDialog1.FileName); Form1_Load(sender, e);
            }
            catch
            {

            }         
        }
//to end a process
        private void button2_Click(object sender, EventArgs e)
        {
         
            foreach (Process p in System.Diagnostics.Process.GetProcesses())
            {
                try
                {
                    if (p.ProcessName.ToString() == listBox1.SelectedItem.ToString())
                    {
                        p.Kill();
                    }
                }
                catch
                {
                }
            }

        }
//to add process in listbox control
        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Process p in System.Diagnostics.Process.GetProcesses())
            {
                listBox1.Items.Add(p.ProcessName);
            }
          

        }
   //timer for refreshing current process
        private void timer1_Tick(object sender, EventArgs e)
        {
        listBox1.Items.Clear();
            foreach (Process p in                        System.Diagnostics.Process.GetProcesses())
            {
                listBox1.Items.Add(p.ProcessName);
            }
        }
    }
}






No comments:

Post a Comment