Note that there are some explanatory texts on larger screens.

plurals
  1. POExecute thread on some object property change
    text
    copied!<p>I make a logging application and I have a LogEvent object with some string properties on it. I want to make this logging asynchronous and in another thread for not blocking the applications GUI thread.</p> <p>Idea is that when I start application, some LogEventThread is running on the background all the time. If LogEvent property has changed then thread is executed, after execution thread suspends and waits another LogEvent object property change and run it again if new property change is captured.</p> <p>Which are the best practises to design this?</p> <p>EDIT:</p> <p>I created an example. Please tell me if I'm on the correct path.</p> <p>I have a Form1:</p> <pre><code>unit MainWindow; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TrackEventSenderThread, Generics.Collections, TrackEvent; type TForm1 = class(TForm) btnTest: TButton; procedure FormCreate(Sender: TObject); procedure btnTestClick(Sender: TObject); private teqTrackEventSenderThread: TTrackEventSenderThread; trackEventQueue: TThreadedQueue&lt;TTrackEvent&gt;; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btnTestClick(Sender: TObject); var trackEvent: TTrackEvent; begin trackEvent := TTrackEvent.Create; trackEvent.Category := 'test'; trackEvent.Action := 'test'; trackEventQueue.PushItem(trackEvent); end; procedure TForm1.FormCreate(Sender: TObject); begin trackEventQueue := TThreadedQueue&lt;TTrackEvent&gt;.Create; teqTrackEventSenderThread := TTrackEventSenderThread.Create(True); teqTrackEventSenderThread.TrackEventQueue := trackEventQueue; teqTrackEventSenderThread.Start; end; end. </code></pre> <p>TrackEvent class:</p> <pre><code>unit TrackEvent; interface type TTrackEvent = class(TObject) private sCategory: string; sAction: string; public property Category: string read sCategory write sCategory; property Action: string read sAction write sAction; end; implementation end. </code></pre> <p>And thread class:</p> <pre><code>unit TrackEventSenderThread; interface uses Classes, Generics.Collections, TrackEvent; type TTrackEventSenderThread = class(TThread) private trackEvent: TTrackEvent; teqTrackEventQueue: TThreadedQueue&lt;TTrackEvent&gt;; public constructor Create(CreateSuspended: Boolean); property TrackEventQueue: TThreadedQueue&lt;TTrackEvent&gt; read teqTrackEventQueue write teqTrackEventQueue; protected procedure Execute; override; end; implementation constructor TTrackEventSenderThread.Create(CreateSuspended: Boolean); begin inherited; end; procedure TTrackEventSenderThread.Execute; begin while not Terminated do begin if teqTrackEventQueue.QueueSize &gt; 0 then begin trackEvent := teqTrackEventQueue.PopItem; //send data to server end; end; end; end. </code></pre>
 

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