Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2006 Sep : Multi threading in Delphi 7

www.cryer.info
Managed Newsgroup Archive

Multi threading in Delphi 7

Subject:Multi threading in Delphi 7
Posted by:"Tom Menderson" (tmenders..@gmail.com)
Date:Tue, 12 Sep 2006 22:08:57

Hi everyone,

I'm new to multi threading so I would appreciate if anyone could help me
with some hints.The scenario is like this: I should have a main thread
manager which starts other threads(child threads). The child threads are
monitoring some folders for new files. When new files are created the
child threads process those files and should not terminate after
processing them because other new files could be created. The child
threads must be signaled from the main thread to terminate and after
that the main thread must terminate itself(I'm not working on the VCL
context):

type
   TMainThread=class(TThread)
   private
     FThread:TWorkingThread;
   protected
     procedure Execute; override;
end;
    
type
   TWorkingThread=class(TThread)
   private
     FoProcessor:TProcessFile;
     FFolder:string;
   protected
     procedure Execute;override;
   public
     constructor Create(FolderToMonitor);
end;

constructor TWorkingThread.Create(FolderToMonitor);
begin
inherited Create;
FFolder:=FolderToMonitor;    
end
            
procedure TMainThread.Execute;
var
   HandleArray : Array[0..15] of THandle;
   ThreadArray : Array[0..15] of TworkingThread;
   i : Integer;
begin
   FreeOnTerminate := True;
   for i := 0 to 15 do
   begin
     //get the folder to process
     ThreadArray[i] := TWorkingThread.Create(FolderToMonitor);
     HandleArray[i] := ThreadArray[i].Handle;
     Sleep(1000);
   end;
   WaitForMultipleObjects(16, @hndlArr, True, INFINITE);
end;

procedure TWorkingThread.Execute;
begin
FreeOnTerminate:=false;
if not Terminated then
   begin
     //get the file to process...
     FoProcessor.ProcessMyFile(FileToProcess);    
   end;
end

My questions are:
1.If I create many TWorkingThreads for each folder I want to monitor, do
I have a separate FoProcessor object for each thread and thus avoid
Syncronize ?
2.How can I signal from TMainThread that all TworkingThreads should
terminate ? Some short example would be very much appreciated...

Thanks in advance,
Tom

Replies:

www.cryer.info
Managed Newsgroup Archive