Ali Keshavarz's Website
RSS icon Email icon Home icon
  • How to use TProcessInfo

    Posted on September 11th, 2009 Ali Keshavarz 5 comments

    The 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.

    Updating running processes list automatically

    TProcessInfo has an internal timer which can update processes list automatically. This timer is controlled by Active, and UpdateInterval properties. Active activates the internal timer, and UpdateInterval specifies update interval. The default value for UpdateInterval is 1000 (1 second).

    To update the list manually, and not using the internal timer, you can call UpdateList method.

    OnBeforeUpdate and OnAfterUpdate

    TProcessInfo has two events. They both are invoked when UpdateList is called, either automatically by internal timer or manually by the programmer.

    OnBeforeUpdate is invoked just before updating the list. It has a Cancel parameter which lets you to cancel updating. OnAfterUpdate is invoked when the list is updated. So if you have a control (e.g. a list view) that should show processes list, you can write its updating code in the event-handler of OnAfterUpdate.

    Finding a running process

    You can iterate over all running processes to find a specific process with specific characteristics, or use FindByID, and FindByName methods to find a process by its Process ID or Process Name:

    var
      Process : TProcessItem;
    begin
      Process := ProcessInfo1.RunningProcesses.FindByID(1234);
      if not Assigned(Process) then
        ShowMessage('No process found');
    end;
    

    Finding a process by its Process ID.

    var
      Process : TProcessItem;
    begin
      Process := ProcessInfo1.RunningProcesses.FindByName('Project3.exe');
      if not Assigned(Process) then
        ShowMessage('No process found');
    end;
    

    Finding a process by its Process Name.

    var
      I: Integer;
      MemSize,
      MaxMemSize : Cardinal;
      ProcessName : string;
    begin
      MaxMemSize := 0;
      for I := 0 to ProcessInfo1.RunningProcesses.Count - 1 do
      begin
        MemSize := ProcessInfo1.RunningProcesses[i].MemoryInfo.WorkingSetSize;
        if MemSize > MaxMemSize then
        begin
          MaxMemSize := MemSize;
          ProcessName := ProcessInfo1.RunningProcesses[i].ExeFile;
        end;
      end;
      ShowMessage(ProcessName + ‘uses more memory than other processes.’);
    end; 

    Finding the process which consumes more memory than others.

    Terminating a running process

    To terminate a running process, you should first find it in the list of running processes, and then call its TerminateProcess method:

     var
      Process : TProcessItem;
    begin
      Process := ProcessInfo1.RunningProcesses.FindByName('notepad.exe');
      if Assigned(Process) then
        Process.TerminateProcess;
    end;
    

    Retrieving full path of a process

     var
      Process : TProcessItem;
    begin
      Process := ProcessInfo1.RunningProcesses.FindByName('notepad.exe');
      if Assigned(Process) then
        ShowMessage(Process.FullPath);
    end;
    

    Listing modules which a process loaded

     var
      Process : TProcessItem;
      I: Integer;
    begin
      Process := ProcessInfo1.RunningProcesses.FindByName('Project1.exe');
      if  Assigned(Process) then
      begin
        for I := 0 to Process.Modules.Count - 1 do
          Memo1.Lines.Add(Format('%d  : %s',[Process.Modules[i].BaseAddress,
                                             Process.Modules[i].ModuleName]));
      end;
    end;
    

    You can access other information about a process (e.g. Threads list, threads count, memory consumption, and so on) in the same way.
    TAppInfo works very similar to TProcessInfo. I will try to explain it more with some examples in another article.

    Share and Enjoy:
    • Print
    • Digg
    • del.icio.us
    • Facebook
    • Google Bookmarks
    • MySpace
    • PDF
    • StumbleUpon
    • Twitter
     

    4 responses to “How to use TProcessInfo” RSS icon

    • Hello, I wonder if you capture information such as the ID of the thread, for example, I have a Thread ID: 1542
      And I want to know the name of the module this thread, if a particular thread of a module so I shall adjourn it.

    • Thanks a lot, but the function “FindProcessByThreadID” is not giving an error when compiled.

      [Error] Unit1.pas(43): Operator not applicable to this operand type
      [Error] Unit1.pas(44): Expression expected but ‘BEGIN’ found
      [Fatal Error] Project1.dpr(6): Could not compile used unit ‘Unit1.pas’

      Would be very grateful if you help me.

      • Adalberto, That sample code is compiled in Delphi 2010. If you are using older versions, especially versions older than Delphi 2007, then you have to change the source code. For example For-in loops are not supported in Delphi 7, and instead of that, you should use a regular For loop:

        for i := 0 to ProcessInfo.RunningProcesses.Count-1 do
        begin
        AProcess := ProcessInfo.RunningProcesses[i];
        // And the rest of code…
        end;


    1 Trackbacks / Pingbacks

    Leave a reply