Difference between revisions of "Asynchronous Calls/ja"
(→問題について) |
m (→問題について: translation error correction) |
||
Line 4: | Line 4: | ||
== 問題について == | == 問題について == | ||
− | + | あるイベントを処理する必要があっても、その場ですぐには処理を実行できない場合があります。例えば、オブジェクトを free しなければいけないのに、後ほど親オブジェクトなどからそのオブジェクトを参照される可能性がある場合です。 | |
− | |||
(日本語訳注:関連記事=http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html ) | (日本語訳注:関連記事=http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html ) | ||
Revision as of 16:08, 17 February 2009
│
English (en) │
français (fr) │
日本語 (ja) │
русский (ru) │
問題について
あるイベントを処理する必要があっても、その場ですぐには処理を実行できない場合があります。例えば、オブジェクトを free しなければいけないのに、後ほど親オブジェクトなどからそのオブジェクトを参照される可能性がある場合です。 (日本語訳注:関連記事=http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html )
解決方法
Application.QueueAsyncCall メソッドを使用してください。
TDataEvent = procedure (Data: PtrInt) of object; procedure QueueAsyncCall(AMethod: TDataEvent; Data: PtrInt);
This will "queue" the given method with the given parameter for execution in the main event loop, when all other events have been processed. In the example above, the reference to the object you wanted to free has gone, since the then-parent has finished execution, and the object you wanted to free can be freed safely.
Note that this is a more generic version of ReleaseComponent, and ReleaseComponent calls this method.
例
次のプログラムは QueueAsyncCall の使い方を示しています。 CallButton を押すと、'Click 1', 'Click 2' そして 'Async 1' が LogListBox に追加されます。注目すべきところは、Async メソッドは、CallButtonClick イベントが終了した後に実行される、という点です。
unit TestQueueAsyncCall; {$mode objfpc}{$H+} interface uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons, StdCtrls; type { TQueueAsyncCallForm } TQueueAsyncCallForm = class(TForm) CallButton: TButton; LogListBox: TListBox; procedure CallButtonClick(Sender: TObject); private { private declarations } FCounter: PtrInt; procedure Async(Data: PtrInt); public { public declarations } end; var QueueAsyncCallForm: TQueueAsyncCallForm; implementation { TQueueAsyncCallForm } procedure TQueueAsyncCallForm.CallButtonClick(Sender: TObject); begin LogListBox.Items.Add('Click 1'); FCounter := FCounter+1; Application.QueueAsyncCall(@Async,FCounter); LogListBox.Items.Add('Click 2'); end; procedure TQueueAsyncCallForm.Async(Data: PtrInt); begin LogListBox.Items.Add('Async '+ IntToStr(Data)); end; initialization {$I testqueueasynccall.lrs} end.