Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
|
prog:lazarus:classes:tthread [13/05/2021 16:05] thierry créée |
prog:lazarus:classes:tthread [23/04/2023 17:44] (Version actuelle) thierry [Appel depuis l'application] |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== TThread ====== | ====== TThread ====== | ||
| + | ===== Titre ===== | ||
| + | ==== Code du Thread ==== | ||
| + | |||
| + | <code delphi code du Thread> | ||
| + | { TMyThread } | ||
| + | procedure TMyThread.Execute; | ||
| + | begin | ||
| + | {... fait quelque chose dans un thread ...} | ||
| + | Synchronize(@SynchroEnum); // Appel une procedure synchronisée avec le thread principal | ||
| + | end; | ||
| + | |||
| + | |||
| + | procedure TMyThread.SynchroEnum; | ||
| + | begin | ||
| + | {... Fait quelque chose synchronisé avec le thread principal ...} | ||
| + | end; | ||
| + | |||
| + | constructor TMyThread.Create(CreateSuspended: boolean); | ||
| + | begin | ||
| + | FreeOnTerminate := True; | ||
| + | inherited Create(CreateSuspended); | ||
| + | end; | ||
| + | |||
| + | </code> | ||
| + | ==== Appel depuis l'application ==== | ||
| + | <code delphi> | ||
| + | FThread := TMyThread.Create(True); // Create Suspended | ||
| + | FThread.OnTerminate:=@ReceptThreadTerminate; | ||
| + | FThread.Start; // et non pas Execute ! | ||
| + | | ||
| + | </code> | ||
| + | ==== A Savoir... ==== | ||
| + | On appel pas directement ''Execute'' mais on démarre le Thread avec ''Start''. | ||
| + | |||
| + | On appel les méthodes "synchronisée" avec ''Synchronize(@MaProcedure)''. Donc il ne peut pas y avoir de paramètres a ces procédures... | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== TThreadList : Partage de TList entre Threads ===== | ||
| + | Avec TThreadList -> [[https://www.freepascal.org/docs-html/rtl/classes/tthreadlist.html]] | ||
| + | |||
| + | |||
| + | |||
| + | |||
| ====== Sources & Ressources ====== | ====== Sources & Ressources ====== | ||
| * [[https://forum.lazarus.freepascal.org/index.php?topic=42198.0|Discussion sur : How to terminate the thread correctly?]] | * [[https://forum.lazarus.freepascal.org/index.php?topic=42198.0|Discussion sur : How to terminate the thread correctly?]] | ||
| + | * [[https://www.freepascal.org/~michael/articles/lazthread/lazthread.pdf|PDF : Introduction to thread programming in Lazarus]] | ||