28 Ağustos 2012 Salı

TProc Kullanımı

{İsmail Kocacan}
unit uTasks;

interface
uses
  SysUtils,
  ExtCtrls,
  Forms;

type
  TTask = class
  private
    FProc: TProc;
    FTimer: TTimer;

    procedure OnTimer(Sender: TObject);
  public
    procedure Run(AProc: TProc);
    procedure Start;
    procedure Stop;
    constructor Create;
    destructor Destroy;
  end;

implementation

{ TTask }
constructor TTask.Create;
begin
  inherited;
  FTimer := TTimer.Create(Application);
  FTimer.Interval := 50000;
end;

procedure TTask.Run(AProc: TProc);
begin
  FProc := AProc;
  FTimer.OnTimer := OnTimer;
end;

procedure TTask.Start;
begin
  FTimer.Enabled := True;
end;

procedure TTask.Stop;
begin
  FTimer.Enabled := false;
end;

destructor TTask.Destroy;
begin
  FTimer.Free;
end;

procedure TTask.OnTimer(Sender: TObject);
begin
  FProc();
end;

end.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;

implementation

uses uTasks;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  ATask: TTask;
begin
  ATask:=TTask.Create;
  ATask.Run(
    procedure
     begin
       Memo1.Lines.Add('Çalışıyor...');
     end
  );
  ATask.Start;
end;

end.

Bir prosedüre referans gösteren tip diye açıklayabiliriz.
Benim örnektede Run Prosedürünün parametresine bir procedüre gövdesi geçtik.
Detaylar SysUtils.pas 3903 den itibaren başlıyor.
İyi Çalışmalar