Solved

Enumerate Modules in a Process the most efficient way

Posted on 2012-04-08
6
210 Views
Last Modified: 2012-04-08
Hi,
In my application I need to retrieve a list of modules that are loaded in a process. The problem is that some of my users have outdated CPUs and will experience a short freezing episode when I enumerate the modules with this code:

       var mProcess = Process.GetProcessById(Scans.ProcId);
                foreach (ProcessModule a in mProcess.Modules)
                {
                    if (!a.FileName.Contains("\\\\."))
                    {
                            //stuff
                    }
                }

Open in new window


Is there another way to do this? Preferably with WMI, but WMI does not have a field for Process Modules, that I know of

Thanks for the time!
0
Comment
Question by:SteveDXL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37821549
0
 

Author Comment

by:SteveDXL
ID: 37821594
Why did you post that thread? I needed to know how to get the process path there, now I need a better way for the modules of the process

sorry if I misunderstood
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37821610
you can always use threadpool to process your code asynchronously, this will not freeze your application

            ThreadPool.QueueUserWorkItem(c =>
                                             {
                                                 var process = Process.GetProcessById(123);

                                                 foreach (ProcessModule module in process.Modules)
                                                 {
                                                   //do your magic here....
                                                 }
                                             });

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SteveDXL
ID: 37821947
Actually the issue is not my application freezing, but the application I'm enumerating is freezing

That will help I think, though. How can I wait for the QueueUserWorkItem to complete?
0
 

Author Comment

by:SteveDXL
ID: 37822341
Final code:

                var resetEvent = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(c =>
                                                 {

                                                     var mProcess = Process.GetProcessById(Scans.MOHAAProcessId);
                                                     var rModules = mProcess.Modules;
                                                     foreach (ProcessModule a in rModules)
                                                     {
                                                         if (!a.FileName.Contains("\\\\."))
                                                         {
                                                           //stuffs
                                                         }
                                                     }
                                                     resetEvent.Set();
                                                 }
                    );
                resetEvent.WaitOne();

Open in new window


Seems to be working fluently - Thanks!
0
 

Author Closing Comment

by:SteveDXL
ID: 37822344
Great thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question