Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi event handling without threads
    primarykey
    data
    text
    <p>I'm not a Delphi expert, and I have only a basic understanding of the flow of messages and the handling of events and callbacks. I'm trying to debug an application in D7 that need to combine the foreground procedure (initiated by a button click) with message reception and processing that occurs in the background (handled by a component). They both work properly independently, but when I combine them in one procedure, the message reception doesn't work. </p> <p>The existing application already has functioning procedures to send a message on a button click, and it can receive a message while running and not processing any buttons. </p> <p>The normal message reception is handled by a component, which calls my handler (OnInput) as an event. My handler puts the message in a ListBox.</p> <p>I am trying to write a procedure that sends a message, and waits for a response, in a loop. (pseudocode):</p> <pre><code> for N := 0 to nsequence do begin prevcount := ListBox1.items.count; SendMessage(mymessage); // request the response sleep(500); // allow time for response to arrive for 0 to timeout do begin sleep(100); application.processmessages; {allow processing to handle incoming message} if ListBox1.items.count &gt; prevcount then break; {has the message arrived?} end; if ListBox1.items.count = prevcount then exit: {timeout -- fail} end; </code></pre> <p>I think this should be able to be accomplished without threads, but the messages are never received. It always times out. </p> <p>Are component event handlers able to be called from application.processmessages? The input message works correctly when the application is running and idle, but does not when it is executing a procedure. What else is required beyond a call to application.processmessages to cause the application to process messages and call the related procedures to handle them?</p> <p>To trace the incoming message, the path starts with a callback from Windows MMSystem. When the MIDI Input port is opened, the callback is set to point to a handler: procedure midiHandler</p> <p>In midiHandler, the event is put into a circular buffer: CircbufPutEvent( thisBuffer, @thisEvent). </p> <p>Then a message is posted back to the application: PostMessage(thisCtlInfo^.hWindow, mim_Data, 0, 0)</p> <p>That message is handled by: procedure TMidiInput.MidiInput( var Message: TMessage ); Within that procedure there is a call to: FOnMIDIInput(Self);</p> <p>These events are defined in the component's interface as:</p> <pre><code>{ Events } FOnMIDIInput: TNotifyEvent; { MIDI Input arrived } </code></pre> <p>under Published, we have:</p> <pre><code>{ Events } property OnMidiInput: TNotifyEvent read FOnMidiInput write FOnMidiInput; property OnOverflow: TNotifyEvent read FOnOverflow write FOnOverflow; </code></pre> <p>In the Object Inspector, the OnMidiInput event is linked to the procedure MIDIInput1MidiInput;</p> <p>The procedure MIDIInput1MidiInput first calls GetMidiEvent:</p> <pre><code>with (Sender As TMidiInput) do begin while (MessageCount &gt; 0) do begin { Get the event as an object } thisEvent := GetMidiEvent; </code></pre> <p>GetMidiEvent reads the message out of the circular buffer. The MIDIInput1MidiInput procedure does some checking and validating, and ultimately stores the message in ListBox1.</p> <p>PS - the input component has a property that returns the count of queued messages in the circular buffer. I check it when the waiting loop times out, and it reports 0 messages. So the callback is apparently not getting to the midiHandler procedure.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload