<?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; Tips &amp; Tricks</title>
	<atom:link href="http://vcldeveloper.com/category/tips-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://vcldeveloper.com</link>
	<description>Ali Keshavarz&#039;s Website</description>
	<lastBuildDate>Mon, 26 Jul 2010 00:37:57 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>How to use TProcessInfo #2</title>
		<link>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo-2/</link>
		<comments>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo-2/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 23:51:07 +0000</pubDate>
		<dc:creator>Ali Keshavarz</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[ProcessInfo]]></category>
		<category><![CDATA[TProcessInfo]]></category>
		<category><![CDATA[دلفی]]></category>

		<guid isPermaLink="false">http://vcldeveloper.com/?p=371</guid>
		<description><![CDATA[I 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 [...]]]></description>
			<content:encoded><![CDATA[<p>I had a few e-mails and comments about using <a href="http://vcldeveloper.com/products/products-components/process-info/" target="_blank">TProcessInfo</a>, so I decided to answer those questions in new blog posts.</p>
<p><strong><span style="color: #ff0000;">Q1</span></strong>: How can we find a module which is running a specific thread, giving a thread ID:</p>
<p><strong>A</strong>: First of all, <span style="text-decoration: underline;">Threads are not assigned to modules</span>, 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:</p>
<pre class="brush: delphi;">
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;
</pre>
<p>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.</p>
<p>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:</p>
<pre class="brush: delphi;">
var
  Process : TProcessItem;
begin
  Process := FindProcessByThreadID(StrToInt(3444));
  if Assigned(Process) then
    ShowMessage(Process.FullPath);
end;
</pre>
<p>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.</p>
<p><strong><span style="color: #ff0000;">Q2</span></strong>: How can we get memory consumption for a given process?</p>
<p><strong>A</strong>: 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 :</p>
<pre class="brush: delphi;">
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;
</pre>
<p>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.</p>
<p><strong><span style="color: #ff0000;">Q3</span></strong>: How can we get process base priority class or change it?</p>
<p><strong>A</strong>: You can access base priority class for a given process or modify it (added in <a href="http://vcldeveloper.com/uncategorized/processinfo-1-3-is-released/" target="_blank">version 1.3</a>), using TProcessItem.</p>
<p>PriorityClassBase. Be careful, adjusting a base priority class of a given process inappropriately might make other processes unresponsive! Here is a example code:</p>
<pre class="brush: delphi;">
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;
</pre>
<p>You can also change each priority of  each thread within a process by setting BasePriority property of that TThreadItem instace.</p>
<p><strong><span style="color: #ff0000;">Q4</span></strong>: How can I check if a process is 64-bit?</p>
<p><strong>A</strong>: You can check this by reading TProcessItem.Is64Bits property.</p>
<p><strong><span style="color: #ff0000;">Q5</span></strong>: How can I get CPU usage for a given process?</p>
<p><strong>A</strong>: You can get CPU usage by reading TProcessItem.CPUUsage property.</p>
<p>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: <a href="http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo/">How to use TProcessInfo</a></p>
<p>If there is any question regarding using Process Info, or anything about Delphi that you think worth being mentioned in Tips &amp; Tricks section of this website, please <a href="http://vcldeveloper.com/contact-me/" target="_blank">contact me</a>, and let me know about it.</p>
<p>Have Fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://vcldeveloper.com/tips-tricks/how-to-use-tprocessinfo-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Component Toolbar slows down RAD Studio 2010</title>
		<link>http://vcldeveloper.com/tips-tricks/component-toolbar-slows-down-rad-studio-2010/</link>
		<comments>http://vcldeveloper.com/tips-tricks/component-toolbar-slows-down-rad-studio-2010/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 19:04:01 +0000</pubDate>
		<dc:creator>Ali Keshavarz</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Component Toolbar]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Delphi 2010]]></category>
		<category><![CDATA[RAD Studio 2010]]></category>
		<category><![CDATA[دلفی]]></category>

		<guid isPermaLink="false">http://vcldeveloper.com/?p=237</guid>
		<description><![CDATA[In 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. Component Toolbar in RAD Studio 2010 This toolbar provides a nice search box which is able to filter components in the palette based on the search phrase. This toolbar [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In <a title="RAD Studio 2010" href="http://www.embarcadero.com/products/rad-studio" target="_blank">RAD Studio 2010</a> there is a new IDE feature called Component Toolbar. This feature provides a component palette similar to the old Delphi component palette.</p>
<p style="text-align: justify;">
<div class="mceTemp" style="text-align: justify;">
<dl id="attachment_241" class="wp-caption alignnone" style="width: 540px;">
<dt class="wp-caption-dt"><a href="http://vcldeveloper.com/wp-content/uploads/2010/01/component_toolbar.png"><img class="size-full wp-image-241 " title="Component Toolbar" src="http://vcldeveloper.com/wp-content/uploads/2010/01/component_toolbar.png" alt="Component Toolbar" width="530" height="55" /></a></dt>
<dd class="wp-caption-dd">Component Toolbar in RAD Studio 2010</dd>
</dl>
</div>
<p style="text-align: justify;">
<p style="text-align: justify;">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:</p>
<p style="text-align: justify;">
<div class="mceTemp" style="text-align: justify;">
<dl id="attachment_242" class="wp-caption alignnone" style="width: 630px;">
<dt class="wp-caption-dt"><a href="http://vcldeveloper.com/wp-content/uploads/2010/01/classic_ide_delphi2010.png"><img class="size-full wp-image-242  " title="classic_ide_delphi2010" src="http://vcldeveloper.com/wp-content/uploads/2010/01/classic_ide_delphi2010.png" alt="" width="620" height="323" /></a></dt>
<dd class="wp-caption-dd">Classic Layout in Delphi 2010</dd>
</dl>
</div>
<p style="text-align: justify;">
<p style="text-align: justify;">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.</p>
<p style="text-align: justify;"><span id="more-237"></span>At first I got suspected to <a title="CnPack" href="http://www.cnpack.org/" target="_blank">CnPack</a> toolbars in form designer and code view, so I disabled them, but it had no effect on the delay!  Then I went for <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" target="_blank">SysInternals Process Monitor</a> to monitor RAD Studio while switching from code view to form designer. There were many Registry activities regarding to component toolbar. At that time I was thinking about standard Tool Palette in RAD Studio 2010, not the classic component toolbar, because I was not using classic layout.</p>
<p style="text-align: justify;">I tried to hide Tool Palette to see if it fixes the delay, but it didn&#8217;t! So I decided to disable its IDE package and see if it fixes the issue. While I was looking into the list of IDE packages in Registry, I found Component Toolbar Package, and I thought this is the package corresponding to Tool Palette. So I removed it from the list of known IDE packages, and restarted RAD Studio 2010; the Tool Palette was still there, BUT the switching delay was gone!</p>
<p style="text-align: justify;">I was wondering if Component Toolbar was not related</p>
<p style="text-align: justify;">to Tool Palette, then what was it related to?! Suddenly I remembered the new classic component toolbar, and switched to Classic Layout. Yes, it was the new classic component toolbar which was using Component Toolbar Package!</p>
<p style="text-align: justify;">Since I don&#8217;t use classic view, I simply disabled this IDE package to have a faster switch time from code view to form designer. But still it is weird why this toolbar is being update even when it is not being visible, and why it is making switching from code view to form designer slower. This is definitely a bug in RAD Studio 2010.</p>
<h2 style="text-align: justify;">How to disable an IDE package:</h2>
<p style="text-align: justify;">Here is how you can disable Component Toolbar Package from Registry:</p>
<p style="text-align: justify;">1- Go to<strong> HKEY_CURRENT_USER\Software\CodeGear\BDS\7.0\Known IDE Packages</strong>.</p>
<p style="text-align: justify;">2- Make a backup by right-clicking on &#8220;Known IDE Packages&#8221; key, and selecting  Export.</p>
<p style="text-align: justify;">3- Find &#8220;<span style="text-decoration: underline;">$(BDS)\bin\comptoolbar140.bpl</span>&#8221; value in &#8220;Known IDE Packages&#8221; key, and delete it.</p>
<p style="text-align: justify;">4- Run RAD Studio 2010.</p>
<p style="text-align: justify;">Regards</p>
]]></content:encoded>
			<wfw:commentRss>http://vcldeveloper.com/tips-tricks/component-toolbar-slows-down-rad-studio-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 15: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[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;">
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;">
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;">
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;"> 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;"> 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;"> 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>
