Solved

Enumerate Modules in a Process the most efficient way

Posted on 2012-04-08
6
208 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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