Skip to content

Instantly share code, notes, and snippets.

@gabr42
Created April 26, 2019 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabr42/15a514a2960be43a0a5b1a6db69274f7 to your computer and use it in GitHub Desktop.
Save gabr42/15a514a2960be43a0a5b1a6db69274f7 to your computer and use it in GitHub Desktop.
InitializedTimedTask
program TimedTask;
uses
Vcl.Forms,
TimedTaskMain in 'TimedTaskMain.pas' {Form89};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm89, Form89);
Application.Run;
end.
object Form89: TForm89
Left = 0
Top = 0
Caption = 'Form89'
ClientHeight = 336
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 24
Top = 64
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object SpinEdit1: TSpinEdit
Left = 24
Top = 24
Width = 121
Height = 22
MaxValue = 0
MinValue = 0
TabOrder = 1
Value = 0
end
object ListBox1: TListBox
Left = 184
Top = 24
Width = 425
Height = 289
ItemHeight = 13
TabOrder = 2
end
end
unit TimedTaskMain;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
OtlTask, OtlParallel;
type
TWorker = class
strict private
FValue: integer;
public
constructor Create(startAt: integer);
procedure Tick;
property Value: integer read FValue;
end;
TForm89 = class(TForm)
Button1: TButton;
SpinEdit1: TSpinEdit;
ListBox1: TListBox;
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
strict private
FTimedTask: IOmniTimedTask;
FWorker: TWorker;
private
public
end;
var
Form89: TForm89;
implementation
{$R *.dfm}
procedure TForm89.FormDestroy(Sender: TObject);
begin
if assigned(FTimedTask) then begin
FTimedTask.Terminate(INFINITE);
FTimedTask := nil;
end;
FreeAndNil(FWorker);
end;
procedure TForm89.Button1Click(Sender: TObject);
begin
Button1.Enabled := false;
FWorker := TWorker.Create(SpinEdit1.Value);
FTimedTask :=
Parallel.TimedTask
.Every(1000)
.Execute(
procedure (const task: IOmniTask)
var
value: integer;
begin
FWorker.Tick;
value := FWorker.Value;
task.Invoke(
procedure
begin
ListBox1.Items.Add(value.ToString);
end);
end);
FTimedTask.Start;
end;
constructor TWorker.Create(startAt: integer);
begin
inherited Create;
FValue := startAt;
end;
procedure TWorker.Tick;
begin
Inc(FValue);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment