how to view processes using windows form, visual studio express C#

Joined
Nov 26, 2006
Messages
620
Ok so I am making a small command center application to control some custom processes. There are three specific processes that I need to be able to monitor, start, stop, and restart from within this form (yes I know I could just look at task manager, but I have something special in mind).

Anyway so I have the visual layout done, I haven't really started any of the backend programming yet... but I want to do this bit first because I have no idea how. I need two boxes on the form one labeled "Running Processes" and "Stopped Processes", I need it to monitor the three specific processes in real time (or updated frequently) and sort them into either the "running processes" box or the "stopped processes" box.

I have seen a tutorial example showing something similar... but its in VB not C# and this app needs to be C#, that and the link to the sample code is broken anyway.

Any thoughts or suggestions would be hella appreciated.
 
VB.NET and C# can be directly translated back and forth.. most of the differences are just in the syntax.

As far as where to start, look into Process class in the System.Diagnostics namespace. With GetProcesses (or GetProcessesByName) you can populate an array with a Process object of every running process (or, say, specific ones). If the process is not found in the array it goes into the "Stopped Processes" list, otherwise the other one.

Then simply use a BackgroundWorker to continually repopulate that array while keeping the UI thread free.
 
Ok so Im a little stuck. I am able to pull the specific processes I want and store them in the Process array. But I am having trouble populating the listbox with the array... I can populate it with a fixed array of strings, but it wont let me populate it with the Process array, it also wont let me convert the process array to an array of strings. A little help would be great! :)
 
Yea Im lost... It doesn't look possible to convert it to an array of strings. I would have to iterate through each element in the process array and find just the name the name and display it. Which I am not entirely sure how to do with list array since you cant print a process type, so I have no idea what the structure of the array is.

Or I could go a different direction entirely. Is there another method out there that would let me simply query the system for a specific process and then return a boolean (like true for running, false for not)? If I could do that then I could just write my own logic for displaying the list of processes.

Any suggestions?
 
The MSDN page shows you what the class returns - a Process struct. It shows you its members and methods, one of which gets the process name as a string.
 
Set the ListBox control's Tag as the Process array. Populate it with the process names, and access a Process object with the SelectedIndex property.

Code:
private void button1_Click(object sender, EventArgs e)
{
	Process[] processes = Process.GetProcesses();
	listBox1.Tag        = processes;

	foreach (Process proc in processes)
	{
		listBox1.Items.Add(proc.ProcessName);
	}
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
	Process[] processes = (Process[])listBox1.Tag;
	textBox1.Text       = processes[listBox1.SelectedIndex].MainWindowTitle.ToString();
}
 
Thanks Sock! Ok that got me a bit further... but I'm still stuck. I need to split the results in two, I need two separate display boxes. I have to filter out the results (looking for my 3 specific processes) and then display the ones that are running in one box and the ones that aren't into another box.

This is what I have now but my boxes aren't displaying anything... the variables "running_list" and "stopped_list" are defined at the class level (I wanted to have the process_viewer method return both lists... but apparently that isn't possible with C#, from what I've read it can only return a single value... correct me if I am wrong). But we can worry about this later, I want to get the basic logic working first. Any help would be awesome.

Code:
private void process_viewer()
        {
            Process[] running_processes = Process.GetProcesses();         
            List<string> process_list = new List<string>();
            List<string> running_list = (List<string>)running.Tag;
            List<string> stopped_list = (List<string>)stopped.Tag;

            foreach (Process proc in running_processes)
            {
                process_list.Add(proc.ProcessName);
            }

            if (process_list.Contains("notepad"))
            {
                this.running_list.Add("Notepad.exe");
            }
            else
            {
                this.stopped_list.Add("Notepad.exe");
            }

            if (process_list.Contains("explorer"))
            {
                this.running_list.Add("Explorer.exe");
            }
            else
            {
                this.stopped_list.Add("Explorer.exe");
            }

            if (process_list.Contains("chrome"))
            {
                this.running_list.Add("Chrome.exe");
            }
            else
            {
                this.stopped_list.Add("Chrome.exe");
            }
            running.DataSource = running_list;
            stopped.DataSource = stopped_list;
        }
 
Last edited:
HA missed the "this"... duh.

should be

running.DataSource = this.running_list;
stopped.DataSource = this.stopped_list;
 
K im back lol... I have a question on backgroundworker. I am having trouble getting it to work... I have tried a few different implementations based on examples online but I just cant seem to get it to work.

In the following code, if I remove the while loop and the backgroundworker code and launch the method directly, it does function perfectly (it just doesn't loop). I have also tried manually threading this function using Thread.Start(mymethod); but I cant get that to work either. With the backgroundworker and while loop code it doesn't ever list the processes, not even the first time... if I use thread.start (and the accompanying logic for that) it will run once correctly but it doesn't update the UI (I have no idea if its looping at all or, if it is but just not updating the UI.)

Guys I need help or this is gonna happen...
head-keyboard.gif


Anyway here is my code, just need a prodding in the right direction...

(the commented out sections are the Thread.Start(); thing I was trying earlier... not sure if I was close or not.)

Code:
public partial class commander : Form
    {
        private List<string> process_list = new List<string>();
        private List<string> running_list = new List<string>();
        private List<string> stopped_list = new List<string>();
public commander()
        {
            InitializeComponent();
           
           //Thread process_viewer_thread = new Thread(process_viewer);
           //process_viewer_thread.Start();

            BackgroundWorker process_viewer_background = new BackgroundWorker();
            process_viewer_background.RunWorkerAsync(process_viewer);
        }

private void process_viewer_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {

                Process[] running_processes = Process.GetProcesses();

                foreach (Process proc in running_processes)
                {
                    this.process_list.Add(proc.ProcessName);
                }

                if (this.process_list.Contains("notepad"))
                {
                    this.running_list.Add("Notepad.exe");
                }
                else
                {
                    this.stopped_list.Add("Notepad.exe");
                }

                if (this.process_list.Contains("explorer"))
                {
                    this.running_list.Add("Explorer.exe");
                }
                else
                {
                    this.stopped_list.Add("Explorer.exe");
                }

                if (this.process_list.Contains("chrome"))
                {
                    this.running_list.Add("Chrome.exe");
                }
                else
                {
                    this.stopped_list.Add("Chrome.exe");
                }

                //stopped.DataSource = this.stopped_list;
                //running.DataSource = this.running_list;

                process_viewer.ReportProgress(100, stopped.DataSource = this.stopped_list);
                process_viewer.ReportProgress(100, running.DataSource = this.running_list);
                this.running_list.Clear();
                this.stopped_list.Clear();
                Thread.Sleep(500);
            }
        }
}
 
Last edited:
Back
Top