Solved

Enumerate Modules in a Process the most efficient way

Posted on 2012-04-08
6
212 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
 

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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