F.Y.I. I tested your thread_sendmessage2 on
Linux debian 3.16.0-4-586 #1 Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24) i686 GNU/Linux
with Lazarus 1.5 r48991M FPC 2.6.5 i386-linux-gtk 2
and Lazarus 1.0.10 through 1.2.4
All work fine (with all 3 methods)
if you don't choose "update and refresh".
However, when choosing "update and refresh" the sendmessage fails in all versions.
(The other 2, synchronize and postmessage keep working, regardless the sleep)
For sendmessage/"update and refresh", just the ProcessMessages doesn't give any problem.
Update and Refresh both give problems.
I did find this post:
SendMessage works different on the gtk+ widgetset than win32.
on Win32. SendMessage sends the message to the queue, and blocks sending thread, until executing thread process the message. If sending thread is the same as process thread, the message is processed immediatly.
But on gtk+ widgetset Send Message is processed directly from the sending thread, instead of putting message to the queue and wait for processing thread to process it! And this cause bugs, especially with GUI applications.
is it a bug or feature?
If this is really true (i.e. the SendMessage is processed
IN the sending thread) then using SendMessage is the same as directly calling handler_send_message from the thread (which is a big no no). So that could be the problem when using SendMessage on Linux.
So policy should be... always use PostMessage or Synchronize.