all groups > c# > april 2008 >
You're in the

c#

group:

Process List / Module List problem..



Process List / Module List problem.. dogatemycomputer@gmail.com
4/24/2008 5:42:15 PM
c#:
I'm new to C#.

I am using the following C# method to populate a TreeView control. If
you call this method once during the life of form then it works
perfectly. If you call the method a second time (without closing and
reloading the app) then the process list populates the TreeView
control with varying degrees of success. Sometimes the process name
shows up (or not), threads that can load modules are red while others
are not and threads that even show during the debug process as being
added to the treeview appear to succeed (and the process name appears
in the treeview control correctly) but the module list never shows up.

I have stepped through the process a couple dozen times and from what
I can gather.. what is appearing in the debug/watch Window is not
making it back to the control.

The formatting and style of the method has been rewritten a dozen
different times today in an attempt to understand where I am going
wrong so it is no longer as elegant as it should be. Once I
understand WHY this is wrong.. I can work on making it look pretty.

I should also note that I know there are other ways to do this. I
would like to know why this particular way behaves in this fashion.

I'm using VS2008 Pro on Vista.


//clear the tree
treeProcessTree.Nodes.Clear();
//add the root
treeProcessTree.Nodes.Add("Process List");

//get a list of processes
Process[] procList = Process.GetProcesses();

//for every process.
for (int i = 0; i < procList.Length; i++)
{
//append the process name to the root
treeProcessTree.Nodes[0].Nodes.Add(procList[i].ProcessName.ToString());

//here we get an array of threads from the process
Process[] ObjModulesList =
Process.GetProcessesByName(procList[i].ProcessName);

//below we try to get a list of resources used by each thread
//then we iterate through each of the resources and append the
filename as a child
//to the the process name node
try
{
ProcessModuleCollection ObjModules = ObjModulesList[0].Modules;
foreach (ProcessModule objModule in ObjModules)
{
treeProcessTree.Nodes[0].Nodes[i].Nodes.Add(objModule.FileName.ToString());
}
}
//some threads return a Win32Exception so we deal with it here
//first we append the error message to the tree then change the
process name and its children to RED
//this way you can tell which modules could not be loaded
catch
{
treeProcessTree.Nodes[0].Nodes[i].Nodes.Add("Error retrieving module
list");
treeProcessTree.Nodes[0].Nodes[i].ForeColor = Color.Red;
treeProcessTree.Nodes[0].Nodes[i].Nodes[0].ForeColor = Color.Red;
}

//obviously some threads are not really threads
if (treeProcessTree.Nodes[0].Nodes[i].Text == "Idle")
{
treeProcessTree.Nodes[0].Nodes[i].ForeColor = Color.Blue;
treeProcessTree.Nodes[0].Nodes[i].Nodes[0].Remove();
}
if (treeProcessTree.Nodes[0].Nodes[i].Text == "System")
{
treeProcessTree.Nodes[0].Nodes[i].ForeColor = Color.
treeProcessTree.Nodes[0].Nodes[i].Nodes[0].Remove();
}

}

treeProcessTree.Sort();
treeProcessTree.Nodes[0].Expand();
procList = null;
Re: Process List / Module List problem.. Peter Duniho
4/24/2008 6:45:51 PM
On Thu, 24 Apr 2008 17:42:15 -0700, dogatemycomputer@gmail.com =

[quoted text, click to view]


Absent a concise-but-complete code sample, along with specific =

instructions as to how to reproduce the problem with that sample, it's =

difficult to see exactly what's not working for you, never mind help =

answer the question.

That said, I'm not a big fan of "it's messy now, I'll clean it up once =

I've figured it all out". Sometimes, the code is too messy to allow a =

hope of figuring things out.

Also, you seem to be using the term "thread" in a complete different way=
=

from what it actually means in the context of Windows. I didn't see an=
y =

code in what you posted that does anything at all with threads.

So, let's look at cleaning up what you've posted. As far as that goes =

(the code, by the way, wasn't indented at all which makes it doubly hard=
=

to examine)...

[quoted text, click to view]
;

Here, you would be better off just saving the return value of the Add() =
=

method, so that you have the current node to work with later. This will=
=

allow you to avoid relying on the exact order of the nodes in the contro=
l =

(you should be able to assume it, but who knows...you can easily avoid t=
he =

assumption, and so you should).

So, something like this:

// Where you add your root node, use something like this:
TreeNode nodeRoot =3D treeProcessTree.Nodes.Add("Process List");

// Then, in the loop (ProcessName is already a string...no need to =
=

convert):
TreeNode nodeProcess =3D treeRoot.Nodes.Add(procList[i].ProcessName=
);

Then, I don't understand at all what you think this is doing:

[quoted text, click to view]

You aren't getting threads here. You're getting another list of =

processes. As near as I can tell, you only ever use the first process i=
n =

the list. At the very least, you run the risk here of using the wrong =

process later, if multiple processes are using the same name (executable=
), =

since no matter which process you're actually adding to the tree, you =

always get the module information from the first process with that name.=


Instead, it seems you can just delete that code and use your current =

process directly later, as in...

[quoted text, click to view]

Instead of "ObjModuleList[0]", just use "procList[i]". After all, that'=
s =

the current process you're working on.

Then, since you've saved the current process node in the tree above, the=
=

following:

[quoted text, click to view]

Can instead look like:

foreach (ProcessModule objModule in ObjModules)
{
nodeProcess.Nodes.Add(objModule.FileName);
}

(Again, the "FileName" property is already a string...no need to convert=
).

You have some remaining comments in the rest of the code that misuse the=
=

term "thread", but hopefully that's been addressed sufficiently that you=
=

can figure that out.

AddThis Social Bookmark Button