Asynchronous Calls/fr: what changed
Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | 1 | # Position du problème | |
| 9 | 2 | ||
| 10 | - | ||
| 3 | + | Quand vous manipulez certains événements, vous avez besoin de faire quelque chose, mais vous ne pouvez le faire correctement. Exemple : vous devez libérer un objet, mais il est ou sera référencé quelque part dans le parent (ou son parent etc.) plus tard. | |
| 11 | 4 | ||
| 12 | 5 | # Solution | |
| 13 | 6 | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 7 | + | Appeler Application.[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]: | |
| 20 | 8 | ||
| 21 | - | ||
| 9 | + | [[doc:lcl/forms/tdataevent.html|TDataEvent]]` = procedure (Data: PtrInt) of object;`\ | |
| 22 | 10 | ||
| 23 | - | ||
| 11 | + | `procedure `[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]`(AMethod: TDataEvent; Data: PtrInt);`\ | |
| 24 | 12 | ||
| 13 | + | Cela placera en file d'attente la méthode donnée avec le paramètre donné pour execution dans la boucle principale des événements, quand tous les autres événements auront été traités. Dans l'exemple du dessus, la référence à l'objet que vous vouliez libérer est partie, depuis le parent d'alors a fini son exécution et l'objet que vous vouliez libérer peut l'être de manière sûre. | |
| 14 | + | ||
| 15 | + | Remarquez qu'il y a une version plus générique de [[doc:lcl/forms/tapplication.releasecomponent.html|ReleaseComponent]], et ReleaseComponent appele cette méthode. | |
| 16 | + | ||
| 25 | 17 | # Exemple | |
| 26 | 18 | ||
| 27 | - | ||
| 28 | - | ||
| 19 | + | Le programme suivant montre l'utilisation de [[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]. Si vous pressez sur le CallButton, 'Click 1', 'Click 2' et 'Async 1' sont ajoutés dans le LogListBox. Remarquez que l'appel Async est seulement exécuté après que l'événement CallButtonClick s'est terminé. | |
| 20 | + | ||
| 21 | + | ``` pascal | |
| 29 | 22 | unit TestQueueAsyncCall; | |
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 23 | + | ||
| 24 | + | {$mode objfpc}{$H+} | |
| 25 | + | ||
| 26 | + | interface | |
| 27 | + | ||
| 28 | + | uses | |
| 29 | + | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons, | |
| 30 | + | StdCtrls; | |
| 31 | + | ||
| 32 | + | type | |
| 33 | + | ||
| 34 | + | { TQueueAsyncCallForm } | |
| 35 | + | ||
| 36 | + | TQueueAsyncCallForm = class(TForm) | |
| 37 | + | CallButton: TButton; | |
| 38 | + | LogListBox: TListBox; | |
| 39 | + | procedure CallButtonClick(Sender: TObject); | |
| 40 | + | private | |
| 41 | + | { private declarations } | |
| 42 | + | FCounter: PtrInt; | |
| 43 | + | procedure Async(Data: PtrInt); | |
| 44 | + | public | |
| 45 | + | { public declarations } | |
| 46 | + | end; | |
| 47 | + | ||
| 48 | + | var | |
| 49 | + | QueueAsyncCallForm: TQueueAsyncCallForm; | |
| 50 | + | ||
| 51 | + | implementation | |
| 52 | + | ||
| 53 | + | {$R *.lfm} | |
| 54 | + | ||
| 55 | + | { TQueueAsyncCallForm } | |
| 56 | + | ||
| 57 | + | procedure TQueueAsyncCallForm.CallButtonClick(Sender: TObject); | |
| 58 | + | begin | |
| 59 | + | LogListBox.Items.Add('Click 1'); | |
| 60 | + | FCounter := FCounter+1; | |
| 61 | + | Application.QueueAsyncCall(@Async,FCounter); | |
| 62 | + | LogListBox.Items.Add('Click 2'); | |
| 63 | + | end; | |
| 64 | + | ||
| 65 | + | procedure TQueueAsyncCallForm.Async(Data: PtrInt); | |
| 66 | + | begin | |
| 67 | + | LogListBox.Items.Add('Async '+ IntToStr(Data)); | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | end. | |
| 78 | 71 | ``` | |
| 79 | 72 | ||
| 80 | 73 | # Voir aussi | |
| 81 | 74 | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 75 | + | - [Fonctionnalité ajoutée dans la 0.9.26. Partie 1. SendMessage et PostMessage](http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html) | |
| 76 | + | - [[Main Loop Hooks/fr|Crochet (hooks) dans la boucle principale]] | |
| 77 | + | - [[Multithreaded Application Tutorial/fr|Tutoriel sur les application Multithreadées]] | |
| 85 | 78 | ||
| 86 | 79 | --- | |
| 87 | - | ||
| 80 | + | *From the Free Pascal wiki, page "Asynchronous Calls/fr", as it stood on 2020-02-09 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
