The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘Delphi’ Category

Delphi multi-threading: confused by TThread.Synchronize / TThread.Queue? You’re not alone. And you need to be aware of exceptions there too.

Posted by jpluimers on 2020/01/01

Below an elaboration on my answer to the question [WayBack] I don’t understand the following part of the second Delphi example:TThread.Synchronize… – Alberto Paganini – Google+:

I was looking at the Task example at the EMB wiki link below

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Tutorial:_Using_Tasks_from_the_Parallel_Programming_Library

and I don’t understand the following part of the second Delphi example:

TThread.Synchronize(nil,
  procedure
  begin
    Label1.Text := lValue.ToString;
  end);

why is there the need to pass Label1.Text := lValue.ToString; as procedure in TThread.Synchronize ?

Why not a simple Label1.Text := lValue.ToString; ?

Basically that question can be either of these:

  1. Why is there no easier language construct than wrapping the callback in a procedure (anonymous method).
  2. Why a call to TThread.Synchronize at all?

The tutorial that Alberto refers to is [WayBack] Tutorial: Using Tasks from the Parallel Programming Library – RAD Studio. That example uses the TTask feature in Delphi, but the portion he has a question about is general to any multi-treading in Delphi that touches the updating of a VCL or FMX UI from another thread.

This is the more complete code in the meant example:

procedure TForm1.ButtonTask1Click(Sender: TObject);
var
  lValue: Integer;
begin
    Label1.Text := '--';
    TTask.Run(procedure
      begin
          {Some calculation that takes time}
          Sleep(3000);
          lValue := Random(10);
          TThread.Synchronize(nil,
            procedure
            begin
                  Label1.Text := lValue.ToString;
            end);
      end);
end;

What happens here is that the TTask is used to run some code (starting with {Some calculation that takes time}) in a different thread (that is being determined by the framework behind TTask).

I recommend against using TTask (or any other part of the Delphi Parallel Programming Library) as I agree with Stefan Glienke in [WayBack] Hello, Do you know why the “default” keyword of a class property is sometime defined also as an attribute ? – Paul TOTH – Google+:

If that works as well as the PPL does I would not touch it with a 10 foot pole ;)

With “that”, he refers to the APL (Asynchronous Programming Library [Archive.is]) which got introduced in Delphi XE8 and adds asynchronous support for UI controls, but unlike .NET (which implements it on the .NET TControl equivalent) shifted it down to TComponent probably because that’s the common ancestor for both VCL and FMX. But that’s a topic for another day.

The Delphi Parallel Programming Library (DPL) is a complex and intricate framework originally started as Delphi Parallel Library (DPL) and modelled after [WayBackIntel’s Threading Building Blocks (TBB) and Microsoft’s Task Parallel Library (TPL) (not in WayBack, but moved to the CHM file [WayBack], see also[WayBack] Task Parallel Library changes since the MSDN Magazine article | Parallel Programming with .NET)

Most parts of the DPL were written over at least 7 years time by former Chief Scientist Allen Bauer and introduced in Delphi XE7 as the Delphi Parallel Programming Library (PPL), see [WayBack] Delphi Parallel Programming Library & Memory Managers – Steve Maughan and [WayBack] The Oracle at Delphi: Lock my Object… Please!.

Since Delphi XE7, the PPL hardly got maintenance and now most if not all of the people having knowledge about it have left Embarcadero: early 2016, [Archive.is] Delphi Chief Scientist Allen Bauer Has Left Embarcadero/Idera | Hacker News leaving a big gap as “There are no plans that I’m aware of to move someone into my old position… All those that I would consider qualified are either already gone or are currently looking elsewhere.” (it is not in the [WayBack] part of How safe is Delphis future? Who is the new Lead Compiler Engineer? Who is the new Chief Scientist? – Ralf Stocker – Google+, hence the screen shot).

Luckily, Allen copied most of his old Embarcadero blog over to his personal one (including comments!), so there is quite some historic reference, see for instance [WayBack] Thread pools <> Task handling. – Community Blogs – Embarcadero Community and [WayBack] The Oracle at Delphi: Thread pools <> Task handling.

So back ensuring you can execute some code on the main thread and the questions from the G+ post:

  1. Why is there no easier language construct than wrapping the callback in a procedure (anonymous method).
  2. Why a call to TThread.Synchronize at all?

Lets start with the questions in order I answered them, starting with the my response:

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

from old school to new frontier: Pattern, naming and MVVM from a Delphi point of view.

Posted by jpluimers on 2019/12/31

On my list of things to check later: FMBC a Delphi implementation of MVVM? [WayBackfrom old school to new frontier: Pattern, naming and MVVM from a Delphi point of view.

via: [WayBack] #FBMC the new MVVM 2.5? – Frank Lauter – Google+

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Delphi, decoding files to strings and finding line endings: some links, some history on Windows NT and UTF/UCS encodings

Posted by jpluimers on 2019/12/31

A while back there were a few G+ threads sprouted by David Heffernan on decoding big files into line-ending splitted strings:

Code comparison:

Python:

with open(filename, 'r', encoding='utf-16-le') as f:
  for line in f:
    pass

Delphi:

for Line in TLineReader.FromFile(filename, TEncoding.Unicode) do
  ;

This spurred some nice observations and unfounded statements on which encodings should be used, so I posted a bit of history that is included below.

Some tips and observations from the links:

  • Good old text files are not “good” with Unicode support, neither are TextFile Device Drivers; nobody has written a driver supporting a wide range of encodings as of yet.
  • Good old text files are slow as well, even with a changed SetTextBuffer
  • When using the TStreamReader, the decoding takes much more time than the actual reading, which means that [WayBack] Faster FileStream with TBufferedFileStream • DelphiABall does not help much
  • TStringList.LoadFromFile, though fast, is a memory allocation dork and has limits on string size
  • Delphi RTL code is not what it used to be: pre-Delphi Unicode RTL code is of far better quality than Delphi 2009 and up RTL code
  • Supporting various encodings is important
  • EBCDIC days: three kinds of spaces, two kinds of hyphens, multiple codepages
  • Strings are just that: strings. It’s about the encoding from/to the file that needs to be optimal.
  • When processing large files, caching only makes sense when the file fits in memory. Otherwise caching just adds overhead.
  • On Windows, if you read a big text file into memory, open the file in “sequential read” mode, to disable caching. Use the FILE_FLAG_SEQUENTIAL_SCAN flag under Windows, as stated at [WayBack] How do FILE_FLAG_SEQUENTIAL_SCAN and FILE_FLAG_RANDOM_ACCESS affect how the operating system treats my file? – The Old New Thing
  • Python string reading depends on the way you read files (ASCII or Unicode); see [WayBack] unicode – Python codecs line ending – Stack Overflow

Though TLineReader is not part of the RTL, I think it is from [WayBack] For-in Enumeration – ADUG.

Encodings in use

It doesn’t help that on the Windows Console, various encodings are used:

Good reading here is [WayBack] c++ – What unicode encoding (UTF-8, UTF-16, other) does Windows use for its Unicode data types? – Stack Overflow

Encoding history

+A. Bouchez I’m with +David Heffernan here:

At its release in 1993, Windows NT was very early in supporting Unicode. Development of Windows NT started in 1990 where they opted for UCS-2 having 2 bytes per character and had a non-required annex on UTF-1.

UTF-1 – that later evolved into UTF-8 – did not even exist at that time. Even UCS-2 was still young: it got designed in 1989. UTF-8 was outlined late 1992 and became a standard in 1993

Some references:

–jeroen

Read the rest of this entry »

Posted in Delphi, Development, Encoding, PowerShell, PowerShell, Python, Scripting, Software Development, The Old New Thing, Unicode, UTF-16, UTF-8, Windows Development | Leave a Comment »

Delphi: Why is there no class procedure TArray.Sort(Keys: array of T; var Values: array of T; const Comparer: IComparer);

Posted by jpluimers on 2019/12/26

The underlying question at [WayBack] … Does anyone know of any array sort method similar to this for Delphi (with the two array parameters, key and value with a comparer method) … – Ugochukwu Mmaduekwe – Google+ was basically this:

Why is there no
class procedure TArray<T>.Sort(Keys: array of T; var Values: array of T; const Comparer: IComparer<T>); overload;

From my answer:

No it is not there as the method that does the actual sort does not accommodate for it:

class procedure TArray.QuickSort<T>(var Values: array of T; const Comparer: IComparer<T>; L, R: Integer);

You could write a helper for it so that similarly to C#, they all end in public static void Sort(Array keys, Array items, int index, int length, IComparer comparer)

In case you need to look at the external TrySZSort: https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/arrayhelpers.cpp#L268

In other cases, C# uses an IntrospectionSort implemented at

Or you could try to use this overload of TArray.Sort Method (array of T):

class procedure Sort<T>(var Values: array of T; const Comparer: IComparer<T>); overload;

It was introduced in Delphi 2009.

Your Comparer then needs to extract the key of each element and implement IComparer<T>.

However, this will never sort the Keys array.

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Delphi 10.1.2 Berlin: after ….

Posted by jpluimers on 2019/12/25

For my archive: [WayBack] Bummer, a big showstopper with Berlin 10.1.2: an object with an indexed property getter crashes the debugger! Please +Marco Cantù we need a fix for this… – André Mussche – Google+

When debugging and adding a watch on a property:

  1. ‘Function to be called, xxxx, was eliminated by linker’
  2. ‘Assertion failure: “!e32->evalArgs.evalFCallPending”
    in ..\win32src\proc32.cpp at line 1830’

It was fixed in 10.2 Tokyo: [RSP-16522] property getters with index can’t be viewed in the debugger and crashes! – Embarcadero Technologies

–jeroen

Posted in Delphi, Delphi 10.1 Berlin (BigBen), Development, Software Development | Leave a Comment »

Flexible and Economical UTF-8 Decoder

Posted by jpluimers on 2019/12/25

For my link archive: [Archive.is] Flexible and Economical UTF-8 Decoder.

Be sure to read the whole article there as the explanation of the initial algorithm is important and final algorithm is towards the end.

The foundation is a state machine combined with a lookup table to find the initial state and proceed to subsequent states.

Related (and reminder to check what David did):

–jeroen

Read the rest of this entry »

Posted in C, C++, Delphi, Development, Software Development | 1 Comment »

Delphi and generics: some examples of “E2015: Operator not applicable to this operand type”

Posted by jpluimers on 2019/12/24

I don’t have enough time right now to elaborate on the code, so below is a an example of where I bumped into the very non-descriptive [WayBack/Archive.is] E2015: Operator not applicable to this operand type when using generics in Delphi.

Most have to do with comparing types (one of which is similar to comparing interfaces where you need to have a GUID in order to get an as comparison working, see Source: Delphi – Using FastMM4 part 2: TDataModule descendants exposing interfaces, or the introduction of a TInterfacedDataModule).

Like most post-Delphi 2007 language features in Delphi, generics still have rough edges. I doubt this will change anytime soon and I am not alone in this.

The documentation never got update to cover situations involving generics ([Archive.is] E2015 Operator not applicable to this operand type (Delphi) – RAD Studio), so basically this to show some examples you might bump into as well.

Note the code below usually is an indication of code-smell, as was the more elaborate situation I had to use it in. A long term solution for that code was to introduce more polymorphism.

A shorter term solution involves either the use of local variables or type-casting (for the latter, see [WayBack] delphi – Cannot compile constrained generic method – Stack Overflow)

–jeroen

Read the rest of this entry »

Posted in Delphi, Development, Software Development | Leave a Comment »

Delphi: 2 things to check when FMX/VCL units are inserted when you use VCL/FMX components

Posted by jpluimers on 2019/12/19

On G+Tom Field bumped in the [WayBack] issue that the IDE would add VCL units to the uses list when putting FMX controls on a form.

I’ve observed it the other way around as well, and there are two things you need to check:

References:

David Nottage:

It has to do with whether there’s {$R *.fmx} or {$R *.dfm} in the unit. You may be able to get away with renaming the .dfm file to .fmx, and changing the directive in the unit.

Jeroen Wiert Pluimers:

 Your .dproj file tells you what kind of application it is (FMX or VCL) in the FrameworkType element and the IDE should respond to that correctly.

–jeroen

Source: [WayBackIn the Berlin Update 2 IDE. I’m working on a multi-device app I’m creating. …

Posted in Delphi, Development, FireMonkey, OS X FMX, Software Development | 3 Comments »

IOTAProjectNotifier.Modified notifies when when Project Options have changed…

Posted by jpluimers on 2019/12/18

From: [WayBack] Is there any way using Open Tools to detect when Project Options have changed? This link from +David Hoyle covers a whole bunch of other notifications: … – David Nottage – Google+

IOTAProjectNotifier.Modified

Note a direct “IOTAProjectNotifier.Modified” – Google Search revealed nothing relevant, but a parts “IOTAProjectNotifier” “Modified” – Google Search revealed [WayBack] RadStudioVersionInsight/SvnIDENotifier.pas at master · rburgstaler/RadStudioVersionInsight · GitHub: TProjectNotifier

Further reading are these excellent blog posts:

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Great quote destructors in Delphi development…

Posted by jpluimers on 2019/12/18

No destructor should ever throw an exception. If it does, there’s not really any way to recover from it anyway, so it doesn’t matter if anything leaks because of it.

Greate quote by [WayBackUser Rob Kennedy answering [WayBackinterface – Avoiding nested try…finally blocks in Delphi – Stack Overflow

It’s a basic development pattern for writing Delphi destructor code.

–jeroen

Posted in Delphi, Design Patterns, Development, Software Development | 6 Comments »