El 13/12/18 a les 20:36, Martin Frb ha escrit:

The thread may makes calls (several, one after the other) into a library, and each of those calls may not return for some time (to long for the main thread to wait, without the app becoming unresponsive). And the structure of that library may not be possible to change.


I think that TThread should have a method to kill it for such cases, using, e.g., pthread_cancel under unix or TerminateThread under windows (though the latter is not a very good option according to the documentation).

Another possible solution for your use case could be to use the OnTerminate callback and ignore the result if it's not your thread, e.g.


FMyThread:=TMyThread.Create(true);
FMyThread.OnTerminate:=@MyTerminate;
FMyThread.Start;

....


procedure TForm1.MyTerminate(Sender:TObject);
var t:tthread;
begin
  if t=FMyThread then
  begin
     //do something with the thread results

    FMyThread:=nil;
  end;
  t.free;
end;


The when you want to ignore the result of the thread, you just set FMyThread to nil. There's a problem though: when the application terminates and the thread is still running, it will call MyTerminate possibly causing a segfault :-/ A solution to that could be to keep a list of running threads so that you can set their OnTerminate to nil when terminating the application and remove the thread of the list in MyTerminate.

Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to