Sign in Join
Wiki copytemporary

Asynchronous Calls/ja

English · Français · Русский · 日本語

問題について

あるイベントを処理する必要があっても、その場ですぐには処理を実行できない場合があります。例えば、オブジェクトを free しなければいけないのに、後ほど親オブジェクトなどからそのオブジェクトを参照される可能性がある場合です。

解決方法

Application.QueueAsyncCall メソッドを使用してください。

pascal
TDataEvent = procedure (Data: PtrInt) of object;

procedure QueueAsyncCall(AMethod: TDataEvent; Data: PtrInt);

これは、任意のメソッドを「キュー」(待ち行列)に入れ、主イベントループ(訳注 : ユーザプログラムの中にあって、ウィンドウシステムから送られてくるイベントを処理するループ。Lazarus等 では隠蔽されている)の中で、他のイベントの処理が全て終わった後で実行するようにします。その際、メソッドに任意のパラメタを渡すことができます。上の例では、free する時点では親の実行が終了していますから、free しようとしているオブジェクトへの参照は消滅しており、安全に free することができるわけです。

これは ReleaseComponent の、よりジェネリックなヴァージョンであり、ReleaseComponent もこのメソッドを呼んでいることにご注意ください。

例

次のプログラムは QueueAsyncCall の使い方を示しています。 CallButton を押すと、'Click 1', 'Click 2' そして 'Async 1' が LogListBox に追加されます。注目すべきところは、Async メソッドは、CallButtonClick イベントが終了した後に実行される、という点です。

pascal
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

{$R *.lfm}

{ 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;

end.

関連項目


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.

Revision 4, last changed by WikiArchive .

Edited on wiki.freepascal.org by 6 people, 2009–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
MalcomeNew page: {{Japanese Menu}} == Problem statement == When handling some event, you need to do something, but you can't do it right away. Example: you need to free an object, but it is or will be ref...
Malcome
Malcome問題について
Malcome問題について
Malcome問題について
Malcome解決方法
Malcome例
Malcome例
Swen (minor)
Malcome例
Malcome問題について
TheCreativeCAT問題について: translation error correction (minor)
TheCreativeCAT解決方法: Translation added (minor)
TheCreativeCAT解決方法: Correction (minor)
TheCreativeCAT解決方法: correction again X-( (minor)
Chronos
Malcome
Malcome問題について
Malcome
Chronos
Yuichiro Takahashi (minor)
Malcome例
TrevFixed syntax highlighting; deleted category already in page template (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.