Asynchronous Calls/ja: what changed
Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## 問題について | |
| 2 | 2 | ||
| 3 | 3 | あるイベントを処理する必要があっても、その場ですぐには処理を実行できない場合があります。例えば、オブジェクトを free しなければいけないのに、後ほど親オブジェクトなどからそのオブジェクトを参照される可能性がある場合です。 | |
| 4 | 4 | ||
| 5 | 5 | ## 解決方法 | |
| 6 | 6 | ||
| 7 | 7 | Application.[[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]] メソッドを使用してください。 | |
| 8 | 8 | ||
| 9 | - | ||
| 9 | + | ``` pascal | |
| 10 | + | TDataEvent = procedure (Data: PtrInt) of object; | |
| 10 | 11 | ||
| 11 | - | ||
| 12 | + | procedure QueueAsyncCall(AMethod: TDataEvent; Data: PtrInt); | |
| 13 | + | ``` | |
| 12 | 14 | ||
| 13 | 15 | これは、任意のメソッドを「キュー」(待ち行列)に入れ、主イベントループ(訳注 : ユーザプログラムの中にあって、ウィンドウシステムから送られてくるイベントを処理するループ。Lazarus等 では隠蔽されている)の中で、他のイベントの処理が全て終わった後で実行するようにします。その際、メソッドに任意のパラメタを渡すことができます。上の例では、free する時点では親の実行が終了していますから、free しようとしているオブジェクトへの参照は消滅しており、安全に free することができるわけです。 | |
| 14 | 16 | ||
| 15 | 17 | これは [[doc:lcl/forms/tapplication.releasecomponent.html|ReleaseComponent]] の、よりジェネリックなヴァージョンであり、ReleaseComponent もこのメソッドを呼んでいることにご注意ください。 | |
| 16 | 18 | ||
| 17 | 19 | ## 例 | |
| 18 | 20 | ||
| 19 | 21 | 次のプログラムは [[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]] の使い方を示しています。 CallButton を押すと、'Click 1', 'Click 2' そして 'Async 1' が LogListBox に追加されます。注目すべきところは、Async メソッドは、CallButtonClick イベントが終了した後に実行される、という点です。 | |
| 20 | 22 | ||
| 21 | 23 | ``` pascal | |
| 22 | 24 | unit TestQueueAsyncCall; | |
| 23 | 25 | ||
| 24 | 26 | {$mode objfpc}{$H+} | |
| 25 | 27 | ||
| 26 | 28 | interface | |
| 27 | 29 | ||
| 28 | 30 | uses | |
| 29 | 31 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons, | |
| 30 | 32 | StdCtrls; | |
| 31 | 33 | ||
| 32 | 34 | type | |
| 33 | 35 | ||
| 34 | 36 | { TQueueAsyncCallForm } | |
| 35 | 37 | ||
| 36 | 38 | TQueueAsyncCallForm = class(TForm) | |
| 37 | 39 | CallButton: TButton; | |
| 38 | 40 | LogListBox: TListBox; | |
| 39 | 41 | procedure CallButtonClick(Sender: TObject); | |
| 40 | 42 | private | |
| 41 | 43 | { private declarations } | |
| 42 | 44 | FCounter: PtrInt; | |
| 43 | 45 | procedure Async(Data: PtrInt); | |
| 44 | 46 | public | |
| 45 | 47 | { public declarations } | |
| 46 | 48 | end; | |
| 47 | 49 | ||
| 48 | 50 | var | |
| 49 | 51 | QueueAsyncCallForm: TQueueAsyncCallForm; | |
| 50 | 52 | ||
| 51 | 53 | implementation | |
| 52 | 54 | ||
| 53 | 55 | {$R *.lfm} | |
| 54 | 56 | ||
| 55 | 57 | { TQueueAsyncCallForm } | |
| 56 | 58 | ||
| 57 | 59 | procedure TQueueAsyncCallForm.CallButtonClick(Sender: TObject); | |
| 58 | 60 | begin | |
| 59 | 61 | LogListBox.Items.Add('Click 1'); | |
| 60 | 62 | FCounter := FCounter+1; | |
| 61 | 63 | Application.QueueAsyncCall(@Async,FCounter); | |
| 62 | 64 | LogListBox.Items.Add('Click 2'); | |
| 63 | 65 | end; | |
| 64 | 66 | ||
| 65 | 67 | procedure TQueueAsyncCallForm.Async(Data: PtrInt); | |
| 66 | 68 | begin | |
| 67 | 69 | LogListBox.Items.Add('Async '+ IntToStr(Data)); | |
| 68 | 70 | end; | |
| 69 | 71 | ||
| 70 | 72 | end. | |
| 71 | 73 | ||
| 72 | 74 | ``` | |
| 73 | 75 | ||
| 74 | 76 | ## 関連項目 | |
| 75 | 77 | ||
| 76 | 78 | - [New 0.9.26 features. Part 1. SendMessage and PostMessage](http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html) | |
| 77 | 79 | - [[Main Loop Hooks]] | |
| 78 | 80 | - [[Multithreaded Application Tutorial/ja|マルチスレッドアプリケーションチュートリアル]] | |
| 79 | 81 | ||
| 80 | 82 | --- | |
| 81 | 83 | *From the Free Pascal wiki, page "Asynchronous Calls/ja", 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).* |
