Invalid Delphi Stream

I am developing a multi-threaded application (RAD Studio XE5). At the beginning of the application, I create one thread that will work until the main form is there.

I can send messages from the stream to any form created in my application, however I cannot find a way to do the opposite by sending a message from the main VCL stream to the workflow.

When creating the main form, I create a workflow and copy the handle in a public variable:

  serverThread := TMyThread.Create(True, ServerPort + 1);
  serverThreadHandle := serverThread.Handle; // SAVE HANDLE
  serverThread.Start;

then (from another FrmSender format) I send a message to the stream:

  PostMessage(uMain.serverThreadHandle, UM_LOC_VCLMSG, UM_LOC_VCLMSG, Integer(PStrListVar));

This is the flow of the procedure:

procedure TMyThread.Execute;
var
    (..)
    vclMSG : TMsg;
    str1, str2 : string;
    (..)
begin
    while not(Terminated) do
    begin
        Sleep(10);
        if Assigned(FrmSender) then
          if FrmSender.HandleAllocated then
            if PeekMessage(vclMSG, FrmSender.Handle, 0, 0, PM_NOREMOVE) then
              begin
                if vclMSG.message = UM_LOC_VCLMSG then
                  begin
                    try
                      pStrListVar := pStrList(vclMSG.lParam);
                      str1 := pStrListVar^.Strings[0];
                      str2 := pStrListVar^.Strings[1];
                    finally
                      Dispose(pStrListVar);
                    end;
                  end;
              end;  
        (.. do other stuff ..)
    end;
end;

However, PeekMessage () never returns true, as if it had never received any message. I tried changing the parameters to PeekMessage () :

PeekMessage(vclMSG, 0, 0, 0, PM_NOREMOVE);

But without results. Any ideas?

+4
2

MSDN PostMessage:

() , , , , , .

, , PostThreadMessage.

, PostThreadMessage:

. , , .

"". . :

  • , .
  • WaitForSingleObject, , PostThreadMessage.
  • , , PeekMessage, , .

    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
    
  • , , .

PeekMessage -1 , .

+5
PeekMessage(vclMSG, FrmSender.Handle, 0, 0, PM_NOREMOVE)

, , , FrmSender.Handle. , uMain.serverThreadHandle. , PeekMessage .

VCL , , . VCL, HandleAllocated Handle. , FrmSender.Handle, .

, . , , , PeekMessage . , PostMessage, .

PostThreadMessage, , , AllocateHWnd.

, PM_NOREMOVE , .

Sleep . GetMessage , . , Sleep, .

Integer 64- . , , - LPARAM.

, , , 2- .

+5

Source: https://habr.com/ru/post/1657513/


All Articles