Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use a Queue of strings with a critical section inside <code>push()</code> and <code>pop()</code>. Inside the thread I would pop strings off, and log them. Inside the GUI thread I would push strings on the queue. I have done something similar before, and it is simple to implement.</p> <hr> <h2>Edit</h2> <p>Interface:</p> <pre><code>TThreadSafeQueue = class(TQueue) protected procedure PushItem(AItem: Pointer); override; function PopItem: Pointer; override; function PeekItem: Pointer; override; end; var CRITICAL_SECTION: TCriticalSection; </code></pre> <p>Implementation:</p> <pre><code>function TThreadSafeQueue.PeekItem: Pointer; begin CRITICAL_SECTION.Enter; Result := inherited PeekItem; CRITICAL_SECTION.Leave; end; function TThreadSafeQueue.PopItem: Pointer; begin CRITICAL_SECTION.Enter; Result := inherited PopItem; CRITICAL_SECTION.Leave; end; procedure TThreadSafeQueue.PushItem(AItem: Pointer); begin CRITICAL_SECTION.Enter; inherited PushItem(AItem); CRITICAL_SECTION.Leave; end; </code></pre> <p>Initialization</p> <pre><code>CRITICAL_SECTION := TCriticalSection.Create; </code></pre> <p>Finalization</p> <pre><code>FreeAndNil(CRITICAL_SECTION); </code></pre> <p>This code uses pointers to objects, but you can create storage for your strings inside the object, using a stringlist or array or whatever best fits your purpose, and change the pop and push methods to operate on your own storage.</p> <hr> <h2>Edit</h2> <p>Something like this:</p> <pre><code>procedure TMyThread.Execute; var Msg: string; begin while not Terminated do begin if FQueue.Count &gt; 0 then begin Msg := FQueue.pop(); PerformLog(Msg); {Whatever your logging method is} end; Sleep(0); end; end; </code></pre>
    singulars
    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.
    1. VO
      singulars
      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