![]() |
![]() ![]() ![]() |
acThread component.
|
property OnExecute: TNotifyEvent;
|
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).
|
|
![]() |
procedure TForm1.ConnectThreadExecute(Sender: TObject);
|
begin
|
{ this can take very long time and suspend the application }
|
mySQLDatabase1.Connected := True;
|
end;
|
procedure TForm1.acThread1Execute(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 = acThread1 then
|
begin
|
FileName1 := Dir + FindData.cFileName;
|
acThread1.Synchronize(SyncFileFound1);
|
end
|
else
|
begin
|
FileName2 := Dir + FindData.cFileName;
|
acThread2.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 TacThread).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;
|
OnException and OnTerminate events.
|