Mega Code Archive

 
Categories / Delphi / Examples
 

Waiting for thread (Revised)

Title: Waiting for thread (Revised) Question: After submitting my previous article (Waiting for thread) (http://www.delphi3000.com/articles/article_2628.asp) a friend called Peter bring me a interesting hint usage of events to control the wait period of the thread Here we go again! Answer: After submitting my previous article (Waiting for thread) (http://www.delphi3000.com/articles/article_2628.asp) a friend called Peter bring me a interesting hint usage of events to control the wait period of the thread Here we go again! Looking in my previous code you will notice that really just hold the execution of the thread bode by counting a number of False Executions on where use I a control variable. In this way the program check for termination more often while hold the thread execution by calling Sleep (from windows API). Implementing a Event to create the same concept require more spare code on different part of the program. I wil affect the Constructor, Destructor and thread execution itself. Event not only involve an clear way to implement a thread but it also release more CPU usage making you program run even smoother than the original version. Implementing Events Adding event capability involves 3 basic API calls. CreateEvent, WaitForSingleObject, and CloseHandle. function CreateEvent(lpEventAttributes: PSecurityAttributes; bManualReset, bInitialState: BOOL; lpName: PChar): THandle; stdcall; function WaitForSingleObject(hHandle: THandle; dwMilliseconds: DWORD): DWORD; stdcall; function CloseHandle(hObject: THandle): BOOL; stdcall; CreateEvent and Close Handle will be placed on the Contructor and Destructor, while WaitForSingleObject will be plased on the thread execution. For those that already have a Sleep call on their code then is better to create a local procedure call Sleep that will be accessible automatically in this way previous code only need to modify the parent class TThread to TexThread. The Code unit ExThread; interface uses Classes, Windows, Sysutils; type TExThread = class(TThread) private { Private declarations } hEvent: THandle; procedure Sleep(Milliseconds:Integer); protected procedure Execute; override; public constructor Create(Suspended:Boolean); destructor Destroy; override; end; implementation constructor TExThread.Create(Suspended: BOolean); var lpEventAttributes: PSecurityAttributes; begin inherited; FillChar(lpEventAttributes,Sizeof(PSecurityAttributes),0); hEvent := CreateEvent(lpEventAttributes,false,False,'EXIT_EVENT'); if hEvent = 0 then raise Exception.Create('Unable to create EXIT_EVENT'); end; destructor TExThread.Destroy; begin if not Terminated then Terminate; SetEvent(hEvent); CloseHandle(hEvent); inherited; end; procedure TExThread.Execute; begin while not terminated do begin { Place thread code here } Sleep(15000) end; end; procedure TExThread.Sleep(Milliseconds:Integer); begin WaitForSingleObject(hEvent,Milliseconds); end; end. New code that inherits from this class will call Sleep normally knowing that the call will return execution in case that the object is released. Thanks to Peter Morris for push the envelope a little bit more. ;-)