Running a method in a thread object


2004-02-19 01:01:35 AM
delphi70
Hi,
I've got a class with a number of different methods, one of which takes a
long time to execute. To keep the UI functional I would like to pass this method
to a thread, something like:
type
TMyMethod = Procedure of Object; // or whatever...
TProcThread = class(TThread)
private
{ Private declarations }
FMyMethod : TMyMethod;
protected
procedure Execute; override;
public
Constructor Create(Suspended: Boolean; Method: TMyMethod);
end;
implementation
{ TProcThread }
constructor TProcThread.Create(Suspended: Boolean; Method: TMyMethod);
begin
FMyMethod := Method;
Inherited Create(Suspended);
end;
procedure TProcThread.Execute;
begin
FMyMethod;
end;
end.
Instead of calling MyObject.LongProc I create a new thread
Thread := TProcThread.create(True, MyObject.LongProc);
and then do some other thread co-ordinating stuff (eg assign an OnTerminate
event handler, have a running thread count variable.), before resuming the
thread.
I'm just wondering if this is a standard way of dealing with this problem,
or if perhaps there are better solutions; and what possible pitfalls I
should be aware of in using this strategy.
Thanks,
Rael