471,354 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Process List / Module List problem..


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;

Jun 27 '08 #1
0 1339

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Åstrand | last post: by
1 post views Thread by | last post: by
10 posts views Thread by Sorin Dolha [MCSD .NET] | last post: by
1 post views Thread by Martin Simard | last post: by
3 posts views Thread by Thomas Dybdahl Ahle | last post: by
3 posts views Thread by Marcin Kalicinski | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.