Shows how to create a simple 'Process Manager'. and control processes
Process Manager
/*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!
*/
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.