Process Manager

tayspen 0 Tallied Votes 182 Views Share

Shows how to create a simple 'Process Manager'. and control processes

/*This program lists all of the current processes on your machine.
*and gives you the option to terminate them
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Resources;
//^ Headers

namespace Process_Manager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Process myProcess = new Process();
            //Declare process
            txtMachine.Text = System.Environment.MachineName.ToString();
            //set the machine name to textbox
        }

        private void FillList(string MachineName)
        {
            Process[] Prc;
            ListViewItem lvwP;

            Cursor.Current = Cursors.WaitCursor;
            //Set cusrsor as wait

            try
            {
                lvwProcesses.Items.Clear();//Clear the items
                Prc = Process.GetProcesses(MachineName.ToString());//Get the process's of our machine

                foreach (Process Prcs in Prc)
                {
                    lvwP = lvwProcesses.Items.Add(Prcs.ProcessName.ToUpper());
                    if (MachineName != System.Environment.MachineName)
                    {
                        //try to add
                        lvwP.SubItems.Add("Unavailable...");
                        lvwP.SubItems.Add("Unavailable...");
                        lvwP.SubItems.Add(Prcs.Id.ToString());
                    }
                    else
                    {
                        //if failed try this
                        lvwP.SubItems.Add(Prcs.MainWindowTitle);
                        lvwP.SubItems.Add(Prcs.Responding.ToString());
                        lvwP.SubItems.Add(Prcs.Id.ToString());
                    }
                }
            }
            catch
            {
                lvwProcesses.Items.Add("Error enumerating items...");//error
            }

            Cursor.Current = Cursors.Default;//defualt cursor
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillList(txtMachine.Text);//fill list
        }

        private void cmdRefresh_Click(object sender, EventArgs e)
        {
            FillList(txtMachine.Text);//refresh
        }
    }
}

/* This could be easily expanded to sstart process's to.
*using  somthing like Process.Start(path);
*Enjoy!
*/

Dani AI

Generated

This is a useful minimal example but a few practical fixes will stop the common "didn't work / error occurred" reports and make the tool safer and more robust. Thanks to for the sample and to for pointing to designer-name issues β€” those are the usual first things to check.

Most likely immediate causes for the errors reported by and

  • Control or event-name mismatch (the designer button name must match the handler; double-check cmdRefresh vs cmdRefersh and the ListView/TextBox names).
  • Namespace/class mismatch between the form and the code-behind.
  • Entering a remote name with leading slashes (use plain machine name, not "\MACHINE").
  • Lack of privileges when trying to enumerate another machine (remote enumeration generally needs administrative rights and remote management enabled).
  • The original catch block swallows exceptions; replace it temporarily to see the real exception message.

Small, safe code suggestions (do not paste the original large block back into designer):

catch (Exception ex)
{
    MessageBox.Show("Enumerate failed: " + ex.Message);
}

To terminate a process prefer polite shutdown first, then force:

try {
    proc.CloseMainWindow();
    if (!proc.WaitForExit(2000)) proc.Kill();
} catch (Exception ex) {
    MessageBox.Show("Cannot terminate " + proc.ProcessName + ": " + ex.Message);
}

Other improvements and cautions

  • Set lvwProcesses.View = View.Details and create columns (Process, Title, Responding, PID). Ensure FullRowSelect true for better UX.
  • Accessing process properties can throw; guard per-process property reads in try/catch and skip inaccessible items.
  • For responsiveness, enumerate on a background thread and marshal updates to the UI with Invoke/BeginInvoke.
  • Remove unused locals like new Process() in the constructor or use using/dispose where appropriate.

Quick checklist: confirm control names and event hooks, show exception messages to find the root cause, run as admin for remote work, and use CloseMainWindow/Kill pattern for termination.

HLA91 -3 Junior Poster

Tried it but didnt work error occured

Ramy Mahrous 401 Postaholic Featured Poster

Thats the process manager....
You've before compiling this code to adjust namespace names or create the windows application with Process_Manager name
second to add in the form ListView control with name lvwProcesses, TextBox control with name txtMachine and Button control with name cmdRefersh and give in click event handler this method cmdRefersh_Click
It will work!

VinhBrao 0 Newbie Poster

the codes are error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.