delphi - How to process an asynchronous queue from within the main UI thread? -
i designing 2 components asynchronously receive objects of custom class (tmelogmessage) , store them in thread-safe internal container. first component non visual (tmefilelogger) , should write info these objects log file (non surprisingly). second component (tmeloggrid) visual fmx.grid descendant should visualize info these objects in ui. these objects is, think, irrelevant.
the problem facing these components not know when these objects enqueued in internal container, have check container see if there new objects need processing, process them , remove them queue. ideally i'd want done when application not busy, in way similar action updating, not bog down ui.
it wrong component hook event handler application.onidle... maybe subscribe tidlemessage, i'm not sure idea, i've read applications never go idle. using internal timer seems bit old-school. maybe use low priority thread poll queue , synchronize ui when find object process. don't have other ideas though.
what proper way in delphi + multiplatform firemonkey?
queue implementations typically implement event (os synchronization object, not vcl 'event') application code can wait on. event set/fired/triggered/however want think of whenever item added empty queue (or, if multiple items added in "batch", after have been added. precise pattern may vary). if queue in case own implementation consider adding such mechanism implementation.
to avoid blocking ui application code creates lightweight thread sole purpose of waiting on queue event, de-queuing items queue ui thread-safe container , notifying ui thread there items processed. monitoring thread resumes waiting event signal there yet more items in queue.
in vcl application mechanism monitoring thread notifies ui naive synchronize
d procedure or (i recommend) message based notification posted form responsible ui processing of items.
note: queue monitoring thread typically responsible handling case application/ui no longer cares processing items (e.g. shutting down) , listens "cancel" or "terminate" event signals thread de-queue items discard them (or deal them in whatever way suits application needs @ time) , terminate (that is, monitoring thread exits).
Comments
Post a Comment