This question includes Delphi and XE specifically designed to pause and resume. I read other posts, and until I found a similar use, so I'm going to go ahead and ask for a discussion.
Which identifier should know if there is a better way to pause a thread when it is not needed?
We have a Delphi class that we have used for many years, which basically represents the FIFO queue, which is associated with the stream process. The queue receives a data object in the main thread, and if the thread is paused, it will resume it.
As part of the thread. The process is running, the object is queued and processed in the thread. This is usually done to search the database.
At the end of the process, the property of the object is updated and marked as available for the main thread or transferred to another queue. The last (well, this is really the first) step of the Execute process is to check if there are more items in the queue. If it continues, otherwise it pauses.
The key is the only suspend action inside the Execute loop when it is complete, and only the resume during normal operations is called when a new item is queued. The exception is when the queue class finishes.
The resume function looks something like this.
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;
Execution is like this
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;
The TthrdQueue Push procedure calls MyResume after adding another object to the stack. MyResume only calls Resume if the thread is paused.
On shutdown, we set the completion to true and call MyResume if it is paused.
multithreading delphi resume
Rich shealer
source share