Calling TThread.Synchronize in main thread

Viewed 3132

Stealing from Uwe Raabe's article Synchronize and Queue with Parameters I do this:

if GetCurrentThreadID = MainThreadID then
  FDataLogger(IntToStr(lNrItems) + ' elements:')
else
  TThread.Synchronize(nil,
          procedure
          begin
            FDataLogger(IntToStr(lNrItems) + ' elements:');
          end);

But if I just

  TThread.Synchronize(nil,
          procedure
          begin
            FDataLogger(IntToStr(lNrItems) + ' elements:');
          end);

that seems to work as well.

Since the first construct leads to more code, is there really much benefit from distinguishing the main thread?

1 Answers

Although the documentation warns you to do that, the actual code inside TThread.Synchronize resembles the wrapping code shown. So I am pretty sure that you can get away with the shorter version.

Perhaps I was also mislead by the documentation (or in ancient times may have been bitten by such a behavior in Delphi 5 or so) when I wrote that article.

Related