By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,710 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,710 IT Pros & Developers. It's quick & easy.

Process List / Module List problem..

P: n/a

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.