thread OnExecute event
Return to Introduction  Previous page  Next page
Applies to
auThread component.  

Declaration
property OnExecute: TNotifyEvent;  

Description
The OnExecute event occurs when the thread ready to execute new process.  
 
Write OnExecute event handler to make some specific operations in the separate process without suspending of main application thread (without blocking of user interface).  

Example 1:
procedure TForm1.ConnectThreadExecute(Sender: TObject);  
begin  
{ this can take very long time and suspend the application }  
  mySQLDatabase1.Connected := True;  
end;  

Example 2:
procedure TForm1.auThread1Execute(Sender: TObject);  
 
  procedure ScanDir(Dir: String);  
  var  
    FindHandle: THandle;  
    FindData: TWin32FindData;  
  begin  
    FindHandle := FindFirstFile(PChar(Dir + '*.*'), FindData);  
    if FindHandle <> INVALID_HANDLE_VALUE then  
     try  
       repeat  
         if (String(FindData.cFileName) <> '.'and  
            (String(FindData.cFileName) <> '..'then  
          begin  
           if Sender = auThread1 then  
            begin  
             FileName1 := Dir + FindData.cFileName;  
             auThread1.Synchronize(SyncFileFound1);  
            end  
           else  
            begin  
             FileName2 := Dir + FindData.cFileName;  
             auThread2.Synchronize(SyncFileFound2);               
            end;  
 
           if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY) then  
             ScanDir(Dir + FindData.cFileName + '\');  
          end;  
       until not FindNextFile(FindHandle, FindData) or (Sender as TauThread).Terminated  
     finally  
       Windows.FindClose(FindHandle);  
    end;  
  end;  
 
begin  
  ScanDir('c:\');  
end;  
 
// methods synchronized with main application thread  
procedure TForm1.SyncFileFound1;  
begin  
  ListBox1.Items.Add(FileName1);  
  Label1.Caption := 'Files: ' + IntToStr(ListBox1.Items.Count);  
end;  
 
procedure TForm1.SyncFileFound2;  
begin  
  ListBox2.Items.Add(FileName2);  
  Label2.Caption := 'Files: ' + IntToStr(ListBox2.Items.Count);  
end;  

See also
OnException and OnTerminate events.