2) never ever call processmessages its the worst think you can do. although lcl makes it impossible you should realy try to avoid it at all costs.
Yeah... lcl makes it impossible but I should avoid it at all costs Nice. And how would I avoid it in this example?
1) what is processmessages? what it does and when is used?
In this case you simple don't need to use it at all. the processing is in an other thread the messages will be processed in due time from the main thread stop disrupting the normal flow of things for no reason.
O wow, for Windows you can indeed remove the ProcessMessages in my simple_postmessage.zip.
BUT... on Linux you
DO need the ProcessMessages. Otherwise the lines in memo1 don't get displayed until ALL messages are handled !!! My simple_postmessage is a simple program without threads but apparently Windows sees the chance to update the memo1 but Linux does not. So the messaging handling in Linux is really really different
I think I understand now why, on Linux, I get the error after 13.000 messages processed. I thought ProcessMessages shouldn't be called in events because you could click a button again but it should also not be called in a message-procedure. Here is why:
Here is the essence of my simple_messages:
procedure TForm1.Button1Click(Sender: TObject);
var
p: integer;
begin
Memo1.Clear; // BUG on Linux - Needs to be clear otherwise it doesn't scrol
for p := 1 to 20000 do
begin
PostMessage(Self.Handle, LM_POST_MESSAGE, p, p);
Button1.Caption := IntToStr(p);
end;
end;
procedure TForm1.post_message(var Msg: TLMessage);
begin
Memo1.Lines.add('p = ' + IntToStr(Msg.wParam) + ' ' + IntToHex(Integer(sPtr),4));
Application.ProcessMessages; // <------- danger of recursion here if there are more LM_POST_MESSAGE on queue
end;
When all the PostMessage are done the handling/processing begins. It runs post_message() and in that I call Application.ProcessMessages (note: on Linux this is necessary to update the memo!). What happens in Linux, I think, is that the App.ProcessMessages calls post_message() again for the second message. And because of the App.ProcessMessages it calls it again for the third.
So we have recursion !! After 13.000 iterations/recursion it crashes with a SIGSEV.
My question is twofold:
1) How can I update the layout/form without calling ProcessMessages (because that recurses into all the other messages) ?
2) Shouldn't ProcessMessages (under Linux) have a mechanisme to prevent such recursion ??
Edit: You can see the recursion happen on Linux when I print out the sPtr (stack pointer) (nibbling 640 bytes from the stack each time). Without the App.ProcessMessages that number stays the same for all calls and with it it changes (until it crashes). On Windows it always stays the same regardless of the App.ProcessMessages. So, somehow Windows avoids the recursion (while the messaging on Linux does not).