On Thu, 24 Apr 2008 17:42:15 -0700, dogatemycomputer@gmail.com =
[quoted text, click to view] <dogatemycomputer@gmail.com> wrote:
>
> 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.=
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] > [...]
> //for every process.
> for (int i =3D 0; i < procList.Length; i++)
> {
> //append the process name to the root
> treeProcessTree.Nodes[0].Nodes.Add(procList[i].ProcessName.ToString())=
;
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] > //here we get an array of threads from the process
> Process[] ObjModulesList =3D
> Process.GetProcessesByName(procList[i].ProcessName);
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] > //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 =3D ObjModulesList[0].Modules;
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] > foreach (ProcessModule objModule in ObjModules)
> {
> treeProcessTree.Nodes[0].Nodes[i].Nodes.Add(objModule.FileName.ToStrin=
g());
> }
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.