Sign in Join
Wiki copytemporary

Asynchronous Calls/fr: what changed

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

OldNewLine
11# Position du problème
22
33Quand 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.
44
55# Solution
66
77Appeler Application.[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]:
88
99[[doc:lcl/forms/tdataevent.html|TDataEvent]]` = procedure (Data: PtrInt) of object;`\
10-\
10+
1111`procedure `[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]]`(AMethod: TDataEvent; Data: PtrInt);`\
1212
1313Cela 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.
1414
1515Remarquez qu'il y a une version plus générique de [[doc:lcl/forms/tapplication.releasecomponent.html|ReleaseComponent]], et ReleaseComponent appele cette méthode.
1616
1717# Exemple
1818
1919Le 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é.
2020
2121``` pascal
2222unit TestQueueAsyncCall;
2323
2424{$mode objfpc}{$H+}
2525
2626interface
2727
2828uses
2929 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,
3030 StdCtrls;
3131
3232type
3333
3434 { TQueueAsyncCallForm }
3535
3636 TQueueAsyncCallForm = class(TForm)
3737 CallButton: TButton;
3838 LogListBox: TListBox;
3939 procedure CallButtonClick(Sender: TObject);
4040 private
4141 { private declarations }
4242 FCounter: PtrInt;
4343 procedure Async(Data: PtrInt);
4444 public
4545 { public declarations }
4646 end;
4747
4848var
4949 QueueAsyncCallForm: TQueueAsyncCallForm;
5050
5151implementation
5252
5353{$R *.lfm}
5454
5555{ TQueueAsyncCallForm }
5656
5757procedure TQueueAsyncCallForm.CallButtonClick(Sender: TObject);
5858begin
5959 LogListBox.Items.Add('Click 1');
6060 FCounter := FCounter+1;
6161 Application.QueueAsyncCall(@Async,FCounter);
6262 LogListBox.Items.Add('Click 2');
6363end;
6464
6565procedure TQueueAsyncCallForm.Async(Data: PtrInt);
6666begin
6767 LogListBox.Items.Add('Async '+ IntToStr(Data));
6868end;
6969
7070end.
7171```
7272
7373# Voir aussi
7474
7575- [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)
7676- [[Main Loop Hooks/fr|Crochet (hooks) dans la boucle principale]]
7777- [[Multithreaded Application Tutorial/fr|Tutoriel sur les application Multithreadées]]
7878
7979---
8080*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