Re: Thread Error in Delphi7
From: Rob Kennedy (me3_at_privacy.net)
Date: 03/25/04
- Next message: Sonny Maou: "Re: Delphi Rox! but..."
- Previous message: Jeremy Collins: "Re: Delphi Rox! but..."
- In reply to: UkkoNoa: "Re: Thread Error in Delphi7"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 25 Mar 2004 11:20:05 -0600
UkkoNoa wrote:
> There is a one unit (analog clock) component containing thread systems and
> the following two-unit own thread in my app.
What need does a clock have for downloading files from the network? I
smell feature creep.
> Implementation
> Uses Some;
>
> var SomeThread :TSome;
Some? You call your class "TSome"? Please pick a meaningful name.
> procedure TMainForm.Some1Click(Sender: TObject);
> begin
> SomeThread := TSome.Create(false);
> end;
Keep in mind that if you click the Some button twice, you will have two
TSome threads, but you'll only have a reference to the second one. The
first reference, which was stored in SomeThread, will get overwritten.
If you don't require any reference to the thread after it's been created
(and it doesn't look like you do), then you don't need to store a
reference to it. You can simply call the constructor and ignore the result:
begin
TSome.Create(False);
end;
> Unit Someunit;
> ===========
> interface
> uses Windows,Classes,Sysutils,Forms,UrlMon,Main;
>
> type
> TSome = class(TThread)
> private
> { Private declarations }
> protected
> procedure Execute; override;
> public
> Constructor Create(CreateSuspended: Boolean);
> procedure CheckUrl;
> end;
>
> implementation
>
> Constructor TSome.Create(CreateSuspended: Boolean);
> Begin
> Inherited Create(CreateSuspended);
> FreeOnTerminate := true;
It's best not to set change the FreeOnTerminate property after the
thread has already started running. Set FreeOnTerminate *before* you
call the inherited constructor. If CreateSuspended is False, then the
thread might already be executing by the time execution reaches the
point where you set the FreeOnTerminate property. And if it's already
executing, then it's already in a position to be raising exceptions and
terminating, so there may be the possibility that you're setting
properties on a thread that's already gone.
> procedure TSome.Execute;
> begin
> Synchronize(CheckUrl);
> end;
That renders your thread totally and completely useless. The Synchronize
procedure is a blocking call. It transfers control back to the main VCL
thread and executes the procedure within that thread's context.
The *only* thing your thread does is wait for the main thread to check a
URL. You could have done that with a simple function call, not a thread.
> (******************************)
> procedure TSome.CheckUrl;
> begin
> if DownloadFromUrl(''xxxxx') then MainForm.Execute;
> Terminate;
> end;
Unless you actually check the Terminated property somewhere else in your
code, the Terminate procedure has no effect. The only time a thread
actually terminates is when its Execute method finishes running. Threads
will usually have some sort of loop in their Execute methods.
You could do this:
type
TSome = class(TThread)
protected
procedure Execute; override;
public
constructor Create(AOnTerminate: TNotifyEvent);
property ReturnValue;
end;
constructor TSome.Create(AOnTerminate: TNotifyEvent);
begin
FreeOnTerminate := True;
OnTerminate := AOnTerminate;
inherited Create(False);
Priority := tpIdle;
end;
procedure TSome.Execute;
begin
if DownloadFromURL('xxxxx') then ReturnValue := 1 else ReturnValue := 0;
end;
procedure TMainForm.Some1Click(Sender: TObject);
begin
TSome.Create(SomeThreadTerminate);
end;
procedure TMainForm.SomeThreadTerminate(Sender: TObject);
begin
case TSome(Sender).WaitFor of
0: ; // DownloadFromURL failed; do nothing.
1: Self.Execute;
else Assert(False, 'TSome terminated with an unexpected value');
end;
end;
-- Rob
- Next message: Sonny Maou: "Re: Delphi Rox! but..."
- Previous message: Jeremy Collins: "Re: Delphi Rox! but..."
- In reply to: UkkoNoa: "Re: Thread Error in Delphi7"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|