Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Process List  / Module List problem..

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dogatemycomputer@gmail.com - 25 Apr 2008 01:42 GMT
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;
Peter Duniho - 25 Apr 2008 02:45 GMT
> I'm new to C#.
>
[quoted text clipped - 7 lines]
> 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 any  
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)...

> [...]
> //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, 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 control  
(you should be able to assume it, but who knows...you can easily avoid the  
assumption, and so you should).

So, something like this:

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

    // Then, in the loop (ProcessName is already a string...no need to  
convert):
    TreeNode nodeProcess = treeRoot.Nodes.Add(procList[i].ProcessName);

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

> //here we get an array of threads from the process
> Process[] ObjModulesList =
> 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 in  
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...

> //below we try to get a list of resources used by each thread
> //then we iterate through each of the resources and append the
[quoted text clipped - 3 lines]
> {
> ProcessModuleCollection ObjModules = 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:

> foreach (ProcessModule objModule in ObjModules)
> {
> treeProcessTree.Nodes[0].Nodes[i].Nodes.Add(objModule.FileName.ToString());
> }

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.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.