Asynchronous Calls/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Asynchronous Calls/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## 問題について | |
| 2 | + | ||
| 3 | + | あるイベントを処理する必要があっても、その場ですぐには処理を実行できない場合があります。例えば、オブジェクトを free しなければいけないのに、後ほど親オブジェクトなどからそのオブジェクトを参照される可能性がある場合です。 | |
| 4 | + | ||
| 5 | + | ## 解決方法 | |
| 6 | + | ||
| 7 | + | 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 | + | これは、任意のメソッドを「キュー」(待ち行列)に入れ、主イベントループ(訳注 : ユーザプログラムの中にあって、ウィンドウシステムから送られてくるイベントを処理するループ。Lazarus等 では隠蔽されている)の中で、他のイベントの処理が全て終わった後で実行するようにします。その際、メソッドに任意のパラメタを渡すことができます。上の例では、free する時点では親の実行が終了していますから、free しようとしているオブジェクトへの参照は消滅しており、安全に free することができるわけです。 | |
| 14 | + | ||
| 15 | + | これは [[doc:lcl/forms/tapplication.releasecomponent.html|ReleaseComponent]] の、よりジェネリックなヴァージョンであり、ReleaseComponent もこのメソッドを呼んでいることにご注意ください。 | |
| 16 | + | ||
| 17 | + | ## 例 | |
| 18 | + | ||
| 19 | + | 次のプログラムは [[doc:lcl/forms/tapplication.queueasynccall.html|QueueAsyncCall]] の使い方を示しています。 CallButton を押すと、'Click 1', 'Click 2' そして 'Async 1' が LogListBox に追加されます。注目すべきところは、Async メソッドは、CallButtonClick イベントが終了した後に実行される、という点です。 | |
| 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 | + | ||
| 74 | + | ## 関連項目 | |
| 75 | + | ||
| 76 | + | - [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 | + | - [[Main Loop Hooks]] | |
| 78 | + | - [[Multithreaded Application Tutorial/ja|マルチスレッドアプリケーションチュートリアル]] | |
| 79 | + | ||
| 80 | + | --- | |
| 81 | + | *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).* |
