Send message just before disconnect, make sure arrives

With closing of my application want to publish some message.
I already use atLeastIonce, now to make sure the message arrives i now have the following construction, i wish for a solution where i can do without application.processmessages.

(if i do the disconnect right after the SendApplicationClosedMessage, the message does not arrive at the broker)

Any idea/suggestion ?

  if FListening then
    FNotifyLastSeenTimer.Enabled := False;
    nu := Now;
    while MQTTInstance.Session.LastReadCommunicationTime<nu do
    FListening := False;

Internally, the MQTT client is thread based. If you send a command, the thread will process this and send it, but if you do a disconnect immediately after send, the thread's execution is immediately stopped and it is likely the message to be sent is not longer processed by the internal thread.
If we change the disconnect behavior to not let it be executed immediately, that might have undesirable effects in other use cases.

I understand.

Would it be an idea, to introduce a Flush method ?
Maybe in combination with the disconnect ?

procedure Disconnect(const AEmptyQueue : Boolean);

Now there is no descent way to make sure, al messages are send.

I make sometimes 2 methods to stop threadbased queue classes,

procedure Stop(); // Stops & processes the queueu until empty
procedure Cancel(); // Stops & forgets about the queue.

(just an example, not that i have to explain that to you guys :) )