-
How to use TProcessInfo #2
Posted on July 26th, 2010 No commentsI had a few e-mails and comments about using TProcessInfo, so I decided to answer those questions in new blog posts.
Q1: How can we find a module which is running a specific thread, giving a thread ID:
A: First of all, Threads are not assigned to modules, they are assigned to processes. You can find a process which is running the thread, by searching in the list of threads owned by each running process. Here is a sample code:
function FindProcessByThreadID(ID: Cardinal): TProcessItem; var ProcessInfo : TProcessInfo; AProcess : TProcessItem; begin Result := nil; ProcessInfo := TProcessInfo.Create(nil); try for AProcess in ProcessInfo.RunningProcesses do begin if Assigned(AProcess.Threads.FindByID(ID)) then begin Result := AProcess; Exit; end; end; finally ProcessInfo.Free; end; end;FindProcessByThreadID iterates over all running processes, and checks which of them has a thread with the given ID. If a process owns a thread with that ID, it will return that process as a TProcessItem object.
Once you have the process object, you can get more information about the process or the specific thread, or you can kill the process, or suspend the thread. For example:
var Process : TProcessItem; begin Process := FindProcessByThreadID(StrToInt(3444)); if Assigned(Process) then ShowMessage(Process.FullPath); end;The above code looks for a thread with its ID = 3444, and if it is found, it provides full path of the EXE file for that process.
Q2: How can we get memory consumption for a given process?
A: You can read memory info for a given process using TProcessItem.MemoryInfo. It provides different info about the memory the process is consuming. Here is an example for reading memory info of Google Chrome process while it is running :
var ProcessInfo : TProcessInfo; AProcess : TProcessItem; begin ProcessInfo := TProcessInfo.Create(nil); try AProcess := ProcessInfo.RunningProcesses.FindByName('Chrome.exe'); if Assigned(AProcess) then ShowMessage(Format('WorkingSet Size = %d, Peak WorkingSet Size = %d', [AProcess.MemoryInfo.WorkingSetSize, AProcess.MemoryInfo.PeakWorkingSetSize])); finally ProcessInfo.Free; end; end;First we search for Chrome.exe process among running processes. Once the process is found, we read its current working set size, and pick working set size through MemoryInfo property, and show them in a message box. The values returned are in Bytes.
Q3: How can we get process base priority class or change it?
A: You can access base priority class for a given process or modify it (added in version 1.3), using TProcessItem.
PriorityClassBase. Be careful, adjusting a base priority class of a given process inappropriately might make other processes unresponsive! Here is a example code:
var ProcessInfo : TProcessInfo; AProcess : TProcessItem; begin ProcessInfo := TProcessInfo.Create(nil); try AProcess := ProcessInfo.RunningProcesses.FindByName('firefox.exe'); if Assigned(AProcess) then AProcess.PriorityClassBase := BELOW_NORMAL_PRIORITY_CLASS; finally ProcessInfo.Free; end; end;You can also change each priority of each thread within a process by setting BasePriority property of that TThreadItem instace.
Q4: How can I check if a process is 64-bit?
A: You can check this by reading TProcessItem.Is64Bits property.
Q5: How can I get CPU usage for a given process?
A: You can get CPU usage by reading TProcessItem.CPUUsage property.
I hope these few answers and sample source codes can help you use Process Info. For other examples of using Process Info, please refer to this post: How to use TProcessInfo
If there is any question regarding using Process Info, or anything about Delphi that you think worth being mentioned in Tips & Tricks section of this website, please contact me, and let me know about it.
Have Fun!
-
Component Toolbar slows down RAD Studio 2010
Posted on January 2nd, 2010 1 commentIn RAD Studio 2010 there is a new IDE feature called Component Toolbar. This feature provides a component palette similar to the old Delphi component palette.
This toolbar provides a nice search box which is able to filter components in the palette based on the search phrase. This toolbar is disabled by default, and is shown when Classic Layout is selected. Here you can see a snapshot of Classic Layout in RAD Studio 2010:
A few weeks ago while I was working with Delphi 2010, I noticed a delay when switching from code view to form designer. The delay was there not only in the complex forms, but also in simple empty forms! It was not a big delay (about 1 second), but since I had to switch between code and form designer view many times, it was really annoying, so I decided to investigate it and find out what is causing this delay.
-
How to use TProcessInfo
Posted on September 11th, 2009 5 commentsThe other day I published Process Info component pack which contains TProcessInfo and TAppInfo. A sample task manager was also published as demo.
I thought maybe it is a good idea to talk about these components and some of their usages by providing some sample source codes. So I will start with TProcessInfo.




