Sign in Join
Wiki copytemporary

Asynchronous Calls/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Asynchronous Calls/fr

OldNewLine
1+# Position du problème
2+
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.
4+
5+# Solution
6+
7+Appeler Application.[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]:
8+
9+[[doc:lcl/forms/tdataevent.html|TDataEvent]]` = procedure (Data: PtrInt) of object;`\
10+\
11+`procedure `[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]`(AMethod: TDataEvent; Data: PtrInt);`\
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+
17+# Exemple
18+
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
22+unit TestQueueAsyncCall;
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.
71+```
72+
73+# Voir aussi
74+
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]]
78+
79+---
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).*

Back to history