Solved

Enumerate Modules in a Process the most efficient way

Posted on 2012-04-08
6
205 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
Comment Utility
0
 

Author Comment

by:SteveDXL
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:SteveDXL
Comment Utility
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
Comment Utility
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
Comment Utility
Great thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now