Category Archives: Advanced

Articles covering advanced topic areas

LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

Useful background to this post is available in post 4 (Linking to an Object for master detail) and post 5 (advanced master detail).

Filtering a TDataSet as detail of an object

Having explored how to link to a Master Detail relationship and created a funky anonymous method to return the data, I thought there is no reason why I should have to be returning linked data. The data could be from anywhere.

One example people have asked about when I’ve been at developer events is how to link an object to be the master for filtering a TDataSet… well, this actually provides a simple example for doing exactly this (as you can see in this video)

Continue reading LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

Please read LiveBindings in VCL – Part 4 – Master Detail before reading this blog / watching the video

In the last blog we looked at how to use master detail and how the BeforeScroll and AfterScoll events worked. I also created using Generics a Sync Object that allowed a single detail object to be linked to.

As promised, this blog post takes it a step further, using the previous foundation I have updated the code (so make sure you download the latest version from code central from the link below) to allow multiple objects of different types to be linked. Additionally, detail lists are also now available.

Link to code sample below…
Continue reading LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

This is the 4th video and blog in a series on using Visual LiveBindings mixed into existing VCL applications. 

In my last post, we looked at how to bind directly in code, using some common Visual LiveBinding Classes and in the next post we will look at more classes that can be used and the difference in the bindings they create. However before we get there a number of you asked about binding to master detail and sub objects. So lets explore this matter.

More in the blog post.

Continue reading LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

Delphi, C++ and InterBase Community

I’m sure some of you have seen the new Delphi, C++ and InterBase community platform that is being developed at Embarcadero. If not I would highly recommend a visit.

The Embarcadero Community is the new home to blogs, event diaries, latest news and features articles. While the old locations are still working, over time expect to see more and more on the community. Community is also social media ready with links for Facebook, Twitter, linkedIn and Google+

Community Welcome Page

Continue reading Delphi, C++ and InterBase Community

Free FireDAC training online

FireDAC Training

Free FireDAC training online!

FireDAC is the Universal Data Access library for developing applications for multiple devices, connected to enterprise databases.

Starting tomorrow you can join the embarcadero team for free FireDAC training online in these special sessions, where you will be taken from a beginner to an expert by showing you how to utilise FireDAC in multiple situations.

Having used multiple database technologies over the years of developing with Delphi, I have been exceptionally impressed by the raw power of FireDAC and how fast and easy it makes the most difficult jobs as a database developer.

The sessions run every Thursday from Jan 22-Mar 26 in 3 time zones so no excuse for missing out!

Register now for free

Date Topic
January 22 Introduction to FireDAC
January 29 Monitoring and Tracing
February 5 Cached Updates and Auto-inc Fields
February 12 Array DML
February 19 Cascading Options System
February 26 Preprocessing
March 5 ETL
March 12 In-Memory Dataset
March 19 Local SQL
March 26 BDE to InterBase Migration

Register now for free

Free XE7 Trial

If your not a XE7 customer you can get the most out of these session by downloading a Delphi XE7 free trial here.

Parallel Programming Thread Pool

In my Parallel Programming introduction post I explored how to easily get performance gains when running loop code by using the TParallel.For() loop construct. A key part of the Parallel Programming Library engine is the new ThreadPool that manages some of the complexity behind the scenes when using this syntax, but more on that later.

Following on from this first post I want to explore a common question I have heard. Is it possible to manage the Parallel Programming library thread pool Size? and if so how?

In short Yes, but I want to pose another question: Should you? – Lets explore this below.

Parallel programming thread pool

The Parallel programming thread pool is very smart! It automatically grows and shrinks based on CPU demand when your application runs and requires its use; it also throttles growth as your CPU usage rises ensuring it doesn’t over cook your CPU and ensuring you don’t lock up your machine. This inbuilt intelligence makes it very efficient and courteous out the box. All of this, without ANY management from us developers! 🙂 So why would you want to change this?

Thats not to say you can’t use a custom(ised) pool. If you do want to limit the size of a pool then you can override the defaults of  a TThreadPool.

TThreadPool Defaults

Defined in System.Threading, TThreadPool initiates with a default of 25 threads per CPU.

MaxThreadsPerCPU = 25;

TThreadPool multiplies the MaxThreadsPerCPU with the number of CPU’s on the machine (it gets this form calling TThread.ProcessorCount) and exposes the result via a read only property TThreadPool.MaxWorkerThreads.

To query the default pool size on your machine at runtime you can use TThreadPool’s handy class method that returns the default pool. With this pool you can then query the MaxWorkerThreads. e.g.

  i : integer;
  i := TThreadPool.Default.MaxWorkerThreads;
  ShowMessage('Pool size = '+i.ToString());

On my Windows VM running 2 cores I see 50, but on my Mac OS X with 8 cores, this code returns 200.

Customising a TThreadPool

Let me start this section by saying, modifying the default TThreadPool properties is not recommended. 

While possible, it is not recommended to modify the Default TThreadPool options as this is a global instance that is used throughout the application, and you never can be sure where and when its being used. You can however create your own instance of a TThreadPool that you modify and use and this is a better approach.

Creating and modifiying a TThreadPool

Creating a TThreadPool is as simple as declaring the variable and calling the constructor.

With an instance of a TThreadPool, you can then modify the MaxWorkerThreads by overriding the value using the method SetMaxWorkerThreads() which takes in an Integer. This sets it at a flat number regardless of the number of CPU’s you have available.  e.g. the following code reports 10 as the Max size on both my Windows and Mac OSX machines mentioned above.

  FPool : TThreadPool;


if Pool = nil then begin
  Pool := TThreadPool.Create;

Note, the MaxWorkerThreads number must always be greater than the MinimumWorkerThreads value that (by default) is set from TThread.ProcessorCount.

A word of caution..

Creating a new TThreadPool come with an overhead. From an initial test where I creating a new TThreadPool for running a small TParallel.For() loop, and then disposing it afterwards it actually decrease performance on your application compared to a traditional for loop. For this reason alone, I would always use a global TThreadPool. When the pool was created globally, the speed performance was immediately backup compared to the create and destroy on demand idea.

 Example of using a custom TThreadPool

Below is an example where Pool is a global TThreadPool.  When the button is selected to run a TThreadPool with a maximum of 10 WorkerThreads. The only adjustments from the example in the previous tutorials is that we now pass in Pool as a paramater to the For loop, note however that this is not being created and free’ed each time in this code.

  Pool: TThreadPool;

procedure TForm5.Button1Click(Sender: TObject);
 Tot: Integer;
 SW: TStopwatch;
 // counts the prime numbers below a given value
 Tot := 0;
 SW := TStopWatch.Create;

 if Pool = nil then begin 
   Pool := TThreadPool.Create;
 TParallel.For(1, Max, procedure (I: Integer)
     if IsPrime (I) then
       TInterlocked.Increment (Tot);
 Memo1.Lines.Add (Format (
 'Parallel (Custom Pool) for loop: %d - %d', [SW.ElapsedMilliseconds, Tot]));

I would say that while this gives me a sense of control, I actually don’t like the fact that I am messing with something that is highly tuned. I would personally conclude that a ThreadPool should be created as the application has initialised and that you use this. Ideally I would say use the default one, as it behaviour is very good already, but if you really want to make more work for yourself, then you can always set the properties of a new pool and use it.

 A thank you to Allen Bauer for his input while writing this post.

NFC in Android from Delphi / Objective Pascal

One great thing about developing with Embarcadero’s RAD Studio, Delphi, C++ Builder and AppMethod is the component development model. Developing with components allows you to write code once that works across all platforms as the components take care of the platform specific API mappings. e.g. Talking to the camera, accelerometer, compass etc.

There are however times when a component doesn’t exist (yet). This is normally when something is platform specific as it doesn’t make sense to have a component that is platform specific when you can still call the API’s of that platform. e.g. prior to the announcements from Apple on the iPhone 6, NFC was on Android but not iOS. 

So what do you need to do when you don’t have a component ready to go? – Well it depends on what you want to call. With regards to accessing NFC, then Brian Long and Danny Magin, both Embarcadero MVP’s, have recently blogged about working through importing the Java libraries required, setting up the intents to collect the message from NFC, setting your application so Android knows it can deal with messages from NFC and then using them. Pretty cool 🙂

If your interested further in calling platform API’s then check out the replays from the Skills Sprints for accessing Android and iOS API’s directly.

TTask.IFuture from the Parallel Programming Library

In my last post I spoke about TTask and how it enables us developers to quickly run multiple tasks at the same time with limited bottleneck in our applications. Moving on from that I want to explore IFuture which impletements ITask.


IFuture , provides TTask with a structure us developers can use to creating a function that returns a specific type (defined using Generics, thats the <T> bit you see in code sometimes).  Using an instance of IFuture, the process can run and then allow us to get other stuff done, until such point as we need the result. This allows us to prioritise code blocks to run in the order we want them to, but still ensure we get the value we need at the point we need it!


To get a value in the future, you first need to define what type of value, set it running and then go call it. To view this, below I am using a totally pointless (but shows how to use this feature) block of code, which I will break down step by step afterwards.

procedure TFormThreading.Button3Click(Sender: TObject);
 OneValue: IFuture <Integer>;
 OtherValue: Integer;
 Total: Integer;
 OneValue := TTask.Future<Integer>(function: Integer
     Result := ComputeSomething;
     Sleep(1000); // delay to show status


 OtherValue := ComputeSomething;


 Total := OtherValue + OneValue.Value;


 // result

The output of this code looks something like this..


Key points in the code

The first step, is using TTask.Future<T> to define the type to be returned, and then pass in the anonymous method to return the instance of that value. (Here we are getting an Integer from ComputeSomething so we use Integer as the type)

The output of calling TTask.Future is an instance of IFuture into the OneValue variable defined.

 OneValue := TTask.Future<Integer>(function: Integer
     Result := ComputeSomething;
     Sleep(1000); // delay to show status

OK, so putting a Sleep command in the anonymous method here is kind of pointless, but it does allow when running this demo code to see the result of the call to OneValue.Status change from WaitingToRun, to Running, to Completed.

As you read down further, you will see OneValue being queried for its current status. The code for converting our Future’s Status to a string is the same as any other Enumeration type, pass in the type you want to convert and the value to GetName.


The first value returned will be WaitingToRun as everything is prepared. Following the first status query, we call the same ComputeSomething task

 OtherValue := ComputeSomething;

Afterwards, we can check the status of OneValue and see that (due to the sleep taking longer than the ComputeSomething call) its now reporting as running.

So hold on! Does that mean we need to check the status to see if its OK to get the value? Well actually NO 🙂

 Total := OtherValue + OneValue.Value;

This line asks OneValue for its Value. If it is done, it will have the value ready for you, if not (as in this case) it will block and wait for IFuture to finish before executing the code making life very easy on us developers.

So thats IFuture, its a process that you can set running, but will return at the point it is ready. Another way to save time and speed up your application code.