Sonjli 6 Posted June 19, 2020 Hello, I am trying to start communication between two threads. The first thread is not mine, it come from an external component library (always Delphi). This component has an event "OnSomethingHappens" that fires in a separate thread. So, in this event I use the OTL Communication system. For example: lMyTask.Comm.Send(WM_MYMESSAGE, MyObject); In my task I do a simple loop like this: while Task.Comm.Receive(lMsg) do begin if lMsg.MsgID = WM_MYMESSAGE then etc. But watching at OTL demos I see that you do somthing like this: repeat case DSiWaitForTwoObjects(task.TerminateEvent, task.Comm.NewMessageEvent, false, task.Param['Delay']) of WAIT_OBJECT_1: begin while task.Comm.Receive(msgID, msgData) do begin if msgID = MSG_CHANGE_MESSAGE then msg := msgData; end; end; WAIT_TIMEOUT: task.Comm.Send(0, msg); else break; //repeat end; until false; Sorry for my ignorance but I don't understand why you use the DSiWaitForTwoObjects and what it means. As I am having some problems in my loop, I think they are related to this use case... so can you explain, please? Thanks again, Eddy Share this post Link to post
Primož Gabrijelčič 223 Posted June 19, 2020 DSiWaitForTwoObjects is just a shortcut which calls WaitForMultipleObjects Windows API with two handles. In this case it will return with status WAIT_TIMEOUT when a timeout elapses, WAIT_OBJECT_1 when task.Comm.NewMessageEvent is signalled or WAIT_OBJECT_0 when task.TerminateEvent is signalled. Share this post Link to post
Sonjli 6 Posted June 19, 2020 Ok, thank you as usual. So, I have this strange behavoiur: The task receiving messages sometimes slow very down, and I can see 30/40 messages queued from the "external" thread, but only one elaboration of my task loop. So in this situation the queue risks to be full very quickly (1000 messages I read in docs). But I don't know why this happens... a loop too thight? Share this post Link to post
Primož Gabrijelčič 223 Posted June 19, 2020 Sorry, impossible to guess 😞 Would need a test app that shows that behaviour to say more. 1 Share this post Link to post
Sonjli 6 Posted June 19, 2020 It's hard to reproduce, yes I understand your pov... I'll try Share this post Link to post