Solved

Enumerate Modules in a Process the most efficient way

Posted on 2012-04-08
6
207 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 42
Web Form VB.Net  import CSV 4 39
VB.NET 2008 - SQL Timeout 9 25
Release Dynamically Allocated Memory in C# 3 28
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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