Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf Suspending a thread in Delphi when it's not needed and safely resuming
    primarykey
    data
    text
    <p>This question involves Delphi and XE specifically deprecating Suspend and Resume. I have read other posts and I have not found a similar usage so far, so I’m going to go ahead and ask for a discussion.</p> <p>What I’d like to know is there a better way to pause a thread when it is not needed?</p> <p>We have a Delphi class that we have used for years that is basically a FIFO Queue that is associated with a threaded process. The queue accepts a data object on the main thread and if the thread is suspended it will resume it.</p> <p>As part of the thread’s Execute process the object is popped out of the queue and processed on the thread. Usually this is to do a database lookup.</p> <p>At the end of the process a property of the object is updated and marked as available to the main thread or passed on to another queue. The last (well it really is the first) step of the Execute process is to check if there are any more items in the queue. If there is it continues, otherwise it suspends itself.</p> <p>They key is the only suspend action is inside the Execute loop when it is completed, and the only resume during normal operations is called when a new item is placed in the queue. The exception is when the queue class is being terminated.</p> <p>The resume function looks something like this.</p> <pre><code>process TthrdQueue.MyResume(); begin if Suspended then begin Sleep(1); //Allow thread to suspend if it is in the process of suspending Resume(); end; end; </code></pre> <p>The execute looks similar to this</p> <pre><code>process TthrdQueue.Execute(); var Obj : TMyObject; begin inherited; FreeOnTerminate := true; while not terminated do begin if not Queue.Empty then begin Obj := Pop(); MyProcess(Obj); //Do work Obj.Ready := true; end else Suspend(); // No more Work end; //Queue clean up in Destructor end; </code></pre> <p>The TthrdQueue Push routine calls MyResume after adding another object in the stack. MyResume only calls Resume if the thread is suspended.</p> <p>When shutting down we set terminate to true and call MyResume if it is suspended.</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.
 

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