[DUG] Terminating a thread
Nick Fauchelle
nick at unica.co.nz
Fri Sep 22 10:06:49 NZST 2006
Indeed, I am wanting to keep the label updated.
And with the TTimer I could keep the label updated every second, but if
I just put
Synchronize(UpdateLabel)
in various places in the Thread then it could go for a few seconds without being updated... which again is why I used a TTimer.
So really, I should move the TTimer out of the thread.
And start it when I call
MyThread := TMyThread.create...
ProgTimer.enabled := false;
and then
MyThread.Terminate;
ProgTimer.enabled := false;
and
procedure TForm1.ProgTimerOnTimer...
begin
//update label
end
Thanks :-)
Nick
Conor Boyd wrote:
> If you're wanting to see how long the thread takes, you'd be better to
> just save the value of Now to a temporary StartTime variable or similar
> before you start your thread, and then in your OnTerminate method you
> can subtract that value from the current value of Now() to get the
> elapsed time.
>
> I don't see why you'd use a TTimer to find out how long something is
> taking. TTimer is for causing an event to fire at particular intervals.
>
> If you want to keep the label updated as your thread executes you could
> have an UpdateLabel method on your form which calculates the elapsed
> time since StartTime, and then from within the loop in your thread's
> Execute method, use Synchronize(UpdateLabel) to actually tell the form
> to update the label.
>
> HTH,
>
> C.
>
> -----Original Message-----
> From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz]
> On Behalf Of Nick Fauchelle
>
> I have the TTimer there so I can have a timer displayed on my form so
> the user can see how long the process has taken. Perhaps I should move
> the Timer out of the Thread and have it on the form instead.... and
> then I guess with my OnTerminate I could synchronize a call to
> ProgTimer.Enabled := false.
>
> Would that be a better option?
>
> I couldn't do the sleep option below as im not trying to Sleep the
> program, more just keep a label updated with a count of the time.
>
> Thanks :-)
>
> Stefan Mueller wrote:
>
>> Timers in a thread sounds indeed very dodgy! - TTimer works with
>> windows messages (messageloop from your mainthread ... and you are
>> calling into your own thread - this is a big no-no!).
>>
>> Usually a "Sleep(1000);" (or better a Sleep(50); inside 20 loops with
>> a check for terminated) in your "Execute" procedure will be a better
>>
> option.
>
>> -----Original Message-----
>> From: delphi-bounces at ns3.123.co.nz
>> [mailto:delphi-bounces at ns3.123.co.nz] On Behalf Of Conor Boyd
>>
>> I'm not sure initially, but you may have to look at having your
>> application (i.e. the main VCL thread) wait at shutdown time until
>> your sub thread has successfully terminated after being told to do so.
>>
>> The other thing is that using Ttimer in a thread may be well dodgy,
>> but that's just a hunch. Why are you having to use a timer?
>>
>> I haven't had to do that myself before, and I'm not sure what the
>> timer is doing that's weird.
>>
>> Look at calling WaitForSingleObject from the main VCL thread or
>> similar, and see if that helps. I can't help you with that straight
>> off, as I haven't had to do that before.
>>
>> Good luck. If I think of anything else, I'll let you know.
>>
>> -----Original Message-----
>> From: delphi-bounces at ns3.123.co.nz
>> [mailto:delphi-bounces at ns3.123.co.nz]
>> On Behalf Of Nick Fauchelle
>>
>> Hmmm ok,
>> So I got that going, and all seems well - my thread has set
>> freeonterminate := true; when it was created.
>> But when I terminate it (try) it seems to work, except when I then
>> close the application I get a Access Violation error..
>>
>> This only happens when I have objects freeing in my destructor like so
>>
>
>
>> begin
>> ProgTimer.Free;
>> ADOQ1.Free;
>> IdHttp.Free;
>> inherited;
>> end;
>>
>> However, if I don't have them there - my ProgTimer (TTimer) still
>> counts the count, even if I terminate the thread...
>> that was created like so in the creator
>> ----
>> ProgTimer := TTimer.Create(nil);
>> ProgTimer.OnTimer := ProgTimerTimer;
>> ProgTimer.Interval := 1000;
>> ProgTimer.Enabled := true;
>>
>> Both ProgTimer : TTimer and it's OnTimer event are declared in the
>> private section of my thread.
>> ---
>>
>> Im thinking it may have something to do with the nil.
>>
>> So,
>> If I terminate, ProgTimer.OnTimer will still fire.
>> If I call ProgTimer.Free with the destructor then the timer stops
>> firing, but the program access violations when I close it.
>>
>
> [snip]
>
>
>
More information about the Delphi
mailing list