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