<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>VCL Developer &#187; Process Info</title>
	<atom:link href="http://vcldeveloper.com/tag/process-info/feed/" rel="self" type="application/rss+xml" />
	<link>http://vcldeveloper.com</link>
	<description>Ali Keshavarz&#039;s Website</description>
	<lastBuildDate>Fri, 04 Nov 2011 23:40:16 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to use TProcessInfo</title>
		<link>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo/</link>
		<comments>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 14:08:29 +0000</pubDate>
		<dc:creator>Ali Keshavarz</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Process Info]]></category>
		<category><![CDATA[ProcessInfo]]></category>
		<category><![CDATA[TProcessInfo]]></category>
		<category><![CDATA[دلفی]]></category>

		<guid isPermaLink="false">http://vcldeveloper.com/?p=173</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I published <a href="http://vcldeveloper.com/products/products-components/process-info/" target="_blank">Process Info component</a> pack which contains TProcessInfo and TAppInfo. A sample task manager was also published as <a title="Download ProcessInfo + Demo" href="http://vcldeveloper.com/downloads/ProcessInfo.zip" target="_blank">demo</a>.</p>
<p>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.</p>
<p><span id="more-173"></span></p>
<h3>Updating running processes list automatically</h3>
<p>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).</p>
<p>To update the list manually, and not using the internal timer, you can call UpdateList method.</p>
<h3>OnBeforeUpdate and OnAfterUpdate</h3>
<p>TProcessInfo has two events. They both are invoked when UpdateList is called, either automatically by internal timer or manually by the programmer.</p>
<p>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.</p>
<h3>Finding a running process</h3>
<p>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:</p>
<pre class="brush: delphi; title: ; notranslate">
var
  Process : TProcessItem;
begin
  Process := ProcessInfo1.RunningProcesses.FindByID(1234);
  if not Assigned(Process) then
    ShowMessage('No process found');
end;
</pre>
<p>Finding a process by its Process ID.</p>
<pre class="brush: delphi; title: ; notranslate">
var
  Process : TProcessItem;
begin
  Process := ProcessInfo1.RunningProcesses.FindByName('Project3.exe');
  if not Assigned(Process) then
    ShowMessage('No process found');
end;
</pre>
<p>Finding a process by its Process Name.</p>
<pre class="brush: delphi; title: ; notranslate">
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 &gt; MaxMemSize then
    begin
      MaxMemSize := MemSize;
      ProcessName := ProcessInfo1.RunningProcesses[i].ExeFile;
    end;
  end;
  ShowMessage(ProcessName + ‘uses more memory than other processes.’);
end; </pre>
<p>Finding the process which consumes more memory than others.</p>
<h3>Terminating a running process</h3>
<p>To terminate a running process, you should first find it in the list of running processes, and then call its TerminateProcess method:</p>
<pre class="brush: delphi; title: ; notranslate"> var
  Process : TProcessItem;
begin
  Process := ProcessInfo1.RunningProcesses.FindByName('notepad.exe');
  if Assigned(Process) then
    Process.TerminateProcess;
end;
</pre>
<h3>Retrieving full path of a process</h3>
<pre class="brush: delphi; title: ; notranslate"> var
  Process : TProcessItem;
begin
  Process := ProcessInfo1.RunningProcesses.FindByName('notepad.exe');
  if Assigned(Process) then
    ShowMessage(Process.FullPath);
end;
</pre>
<h3>Listing modules which a process loaded</h3>
<pre class="brush: delphi; title: ; notranslate"> 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;
</pre>
<p>You can access other information about a process (e.g. Threads list, threads count, memory consumption, and so on) in the same way.<br />
TAppInfo works very similar to TProcessInfo. I will try to explain it more with some examples in another article.</p>
]]></content:encoded>
			<wfw:commentRss>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/9 queries in 0.045 seconds using disk: basic
Object Caching 352/364 objects using disk: basic

Served from: vcldeveloper.com @ 2012-02-04 21:47:19 -->
