How to create a Task Manager in C#

By FoxLearn 11/19/2024 3:17:34 PM   8.98K
To create a simple Task Manager in C# Windows Forms that can kill a process, you can follow these steps.

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "TaskManager" and then click OK

Drag and drop the ListBox, Button controls from the Visual Toolbox onto your form designer, then design your form as below

Your main form name: Form1

task manager in c#

Your run new task name: frmRunTask

create task manager in c#

Add code to handle your form as below

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Process[] proc;

       
        void GetAllProcess()
        {
            // Get all running process
            proc = Process.GetProcesses();
            // Clear the listbox
            listBox.Items.Clear();
            // add processes into ListBox
            foreach (Process p in proc)
                listBox.Items.Add(p.ProcessName);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetAllProcess();
        }

        private void btnEndTask_Click(object sender, EventArgs e)
        {
            try
            {
                // Kill the selected process
                proc[listBox.SelectedIndex].Kill();
                // Refresh the process list
                GetAllProcess();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void runNewTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using(frmRunTask frm=new frmRunTask())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                    GetAllProcess();
            }
        }
    }
}

When the form loads, it calls GetAllProcess() to populate the ListBox with the names of all running processes.

btnEndTask_Click: This is the event handler for when the "End Task" button is clicked. It gets the selected process name from the ListBox, finds the corresponding process using Process.GetProcessesByName(), and calls Kill() on it. After the process is killed, it refreshes the process list.

frmRunTask

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class frmRunTask : Form
    {
        public frmRunTask()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            // Check if the text box is not empty
            if (!string.IsNullOrEmpty(txtOpen.Text))
            {
                try
                {
                    // Create a new process to run the entered file
                    Process proc = new Process();
                    proc.StartInfo.FileName = txtOpen.Text; // Set the file name to run
                    proc.Start(); // Start the process
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}

The `frmRunTask` form enables the user to launch a new process by entering the executable or file path into a text box and clicking a button to run it.

VIDEO TUTORIAL

Related