Главная страница | назад





Article #25707: Creating a Worker Thread for Background Processing

Problem:
Creating a Worker Thread for Background Processing
Solution:
This information in this document applies to:
* InterBase 5.x
* Delphi 32-bit
*******************************************
Steps to Create a Background Thread
*******************************************
1.Create new Application
2.Create a descendant of TThread (we'll call it TWorkerThread). You can do this by
selecting the File | New menu option and choosing the "Thread Object" item.
3.Save your unit (we'll call it WorkerThreadUnit)
4.Add WorkerThreadUnit to interface uses section of TMainForm. This is added to the
interface section, opposed to the implementation section, because we are going to
create a variable of this class and need this when declaring the main form's class.
{ begin code snippet }
interface
uses
WorkerThreadUnit;
{ end code snippet }
5.Create a variable of TWorkerThread. Here is a code snippet showing a variable of
type TWorkerThread being declared.
{ begin code snippet }
type
TMainForm = class(TForm)
private
public
{ Public declarations }
workerThread: TWorkerThread;
end;
{ end code snippet }
6. Add TMainForm's unit (MainUnit) to the implementation section of
WorkerThreadUnit. We add it to the implementation section, because we will use it
in our classes declaration, not its definition.
{ begin code snippet }
implementation
uses SysUtils, MainUnit;
{ end code snippet }
7. Fill in the Execute method of TWorkerThread to do what you want when thread is
created. This method will be called when you create an instance of your thread.
{ begin code snippet }
procedure TWorkerThread.Execute;
begin
{ Place thread code here }
.
.
.
end;
{ end code snippet }
8. Create a method to be used for all your user interface code. This needs to be in a
separate method so that we can invoke it with the Synchronize method of the
TThread class (we'll call it UpdateUI)
{ begin code snippet }
procedure TWorkerThread.UpdateUI;
begin
{ do user interface stuff here }
.
.
.
end;
{ end code snippet }
9. In TWorkerThread's Execute method call the Synchronize method and pass it the
UpdateUI method as a parameter.
{ begin code snippet }
procedure TWorkerThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate := true;
Synchronize(UpdateUI);
end;
{ end code snippet }
10. Now, in your main form create an instance of your TWorkerThread class where ever
you need a new thread. Here is an example showing an instance of our
TWorkerThread class being created when Button1 is clicked.
{ begin code snippet }
procedure TMainForm.Button1Click(Sender: TObject);
begin
workerThread := TWorkerThread.Create(false);
end;
{ end code snippet }
********
Notes
********
* Delphi requires (there are a few exceptions) that all user interface code be executed in
the context of the application's primary thread.
* TThread provides a method called Synchronize to allow some of its methods to be
executed in the context of the application's primary thread.
* TThread is an object pascal object that encapsulates a Win32 API thread object.
The parameter to the TThread.Create method controls whether the thread is created
in a suspended state. If true is passed in then the thread will be created, but its execute
will be suspended. If false is passed in the thread will be created and will execute
immediately. In the above example I passed in false so that the worker thread would
execute as soon as it was created.
* The thread associated with a TThread object will be terminated as soon as the
TThread.Execute method is done executing.
* However, the TThread object will not automatically be destroyed when the
underlying thread terminates. To destroy the TThread object you can:
* Explicitly call the TThread.Destroy method
* Set the TThread's property FreeOnTerminate to true in the TThread.Execute
method. If FreeOnTerminate is true then the TThread object will be
automatically destroyed when the underlying thread is terminated.

Last Modified: 23-OCT-00