[fpc-pascal] TThread.WaitFor not returning?

papelhigienico at gmail.com papelhigienico at gmail.com
Wed Sep 17 00:28:46 CEST 2008


I have a similar problem when using threads with synchronize...  And I
understand that threads in FPC/Lazarus under Linux/BSD works like this:

When you call synchronize from inside of you thread, this sends a message to
application thread with the procedure that will be used to synchronize AND
WAIT FOR a response from application thread of the execution of sync
procedure. BUT when you call Thread.WaitFor in application thread, the
application don't process any messages because it is waiting the thread
finish, and thread don't finish because it is waiting a response of
application thread of the execution of sync procedure... and you have your
aplication freeze!

My solution in this case is override the WaitFor procedure...

procedure TMyThread.WaitFor;
   while not ended do

...and var ended must be set to true in the last line of procedure

Fabio Luis Girardi

2008/9/16 Luca Olivetti <luca at ventoso.org>

> En/na Graeme Geldenhuys ha escrit:
>> procedure TtiLogToCacheAbs.Terminate;
>> begin
>>  writeln('>> TtiLogToCacheAbs.Terminate');
>>  FThrdLog.Terminate;
>>  writeln('   called .Terminate() and now calling .WaitFor()');
>>  FThrdLog.WaitFor;
>>  writeln('<< TtiLogToCacheAbs.Terminate');
>> end;
>> Any ideas?
> Do you call synchronize inside the thread?
> Anyway, I don't usually call WaitFor (I've had many problems with it), I
> set a flag in the thread and I loop sleeping (or calling checksynchronize if
> the thread uses synchronize) until it's true, eg:
> procedure TMyThread.execute;
> begin
>  while not terminated do
>  begin
>    ....
>  end;
>  finished:=true;
> end;
> MyThread.Terminate;
> while not MyThread.Finished do sleep(100);
> or
> while not MyThread.Finished do CheckSynchronize(100);
> Bye
> --
> Luca
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20080916/26f35a87/attachment.html>

More information about the fpc-pascal mailing list