Freddy1990
11-19-2006, 09:59 PM
In this tutorial we will learn how to add and use the TTimer component in SCAR forms.
Timers can be used for multithreading, each timer represents a separed thread.
We begin with a basic script that shows a form:
program TimerTest
var
frmDesign: TForm;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
end.
http://stuart.existhost.com/freddy/tutimages/menu1.PNG
Timer components are invisible, so this is the only pic i'll be needing :)
We're going to add 2 timers and make each of them run a procedure on different intervals.
First we declare the timer componenet variables:
tmr1, tmr2: TTimer;
Now we create the timer objects in our form:
tmr1 := TTimer.Create(frmDesign);
tmr2 := TTimer.Create(frmDesign);
The 2 different intervals will be 500ms and 1000ms, so we're going to set outr timers with those intervals:
tmr1.Interval := 500;
tmr2.Interval := 1000;
Now we will need some procedures to run with the timers, these are just like the ones used with onclick events:
procedure Timer1(sender: TObject);
begin
WriteLn('Timer1 looping!');
end;
procedure Timer2(sender: TObject);
begin
WriteLn('Timer2 looping!');
end;
While we use the onclick event for buttons, timers use the ontimer event, so we add the procedures to that event:
tmr1.OnTimer := @Timer1;
tmr2.OnTimer := @Timer2;
We end with the followig script:
program TimerTest;
var
frmDesign: TForm;
tmr1, tmr2: TTimer;
procedure Timer1(sender: TObject);
begin
WriteLn('Timer1 looping!');
end;
procedure Timer2(sender: TObject);
begin
WriteLn('Timer2 looping!');
end;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
tmr1 := TTimer.Create(frmDesign);
tmr1.Interval := 500;
tmr1.OnTimer := @Timer1;
tmr2 := TTimer.Create(frmDesign);
tmr2.Interval := 1000;
tmr2.OnTimer := @Timer2;
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
Now when we run the script we'll get a nice output, for every time it says "Timer2 looping!", it says "Timer1 looping!" twice.
Timer1 looping!
Timer1 looping!
Timer2 looping!
Timer1 looping!
Timer1 looping!
Timer2 looping!
Timer1 looping!
Timer1 looping!
Timer2 looping!
That was it, have fun!
Timers can be used for multithreading, each timer represents a separed thread.
We begin with a basic script that shows a form:
program TimerTest
var
frmDesign: TForm;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
end.
http://stuart.existhost.com/freddy/tutimages/menu1.PNG
Timer components are invisible, so this is the only pic i'll be needing :)
We're going to add 2 timers and make each of them run a procedure on different intervals.
First we declare the timer componenet variables:
tmr1, tmr2: TTimer;
Now we create the timer objects in our form:
tmr1 := TTimer.Create(frmDesign);
tmr2 := TTimer.Create(frmDesign);
The 2 different intervals will be 500ms and 1000ms, so we're going to set outr timers with those intervals:
tmr1.Interval := 500;
tmr2.Interval := 1000;
Now we will need some procedures to run with the timers, these are just like the ones used with onclick events:
procedure Timer1(sender: TObject);
begin
WriteLn('Timer1 looping!');
end;
procedure Timer2(sender: TObject);
begin
WriteLn('Timer2 looping!');
end;
While we use the onclick event for buttons, timers use the ontimer event, so we add the procedures to that event:
tmr1.OnTimer := @Timer1;
tmr2.OnTimer := @Timer2;
We end with the followig script:
program TimerTest;
var
frmDesign: TForm;
tmr1, tmr2: TTimer;
procedure Timer1(sender: TObject);
begin
WriteLn('Timer1 looping!');
end;
procedure Timer2(sender: TObject);
begin
WriteLn('Timer2 looping!');
end;
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 259;
frmDesign.Top := 132;
frmDesign.Width := 354;
frmDesign.Height := 254;
frmDesign.Caption := 'Menu Testing Form';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
tmr1 := TTimer.Create(frmDesign);
tmr1.Interval := 500;
tmr1.OnTimer := @Timer1;
tmr2 := TTimer.Create(frmDesign);
tmr2.Interval := 1000;
tmr2.OnTimer := @Timer2;
end;
procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(v, 0);
ThreadSafeCall('ShowFormModal', v);
end;
begin
SafeInitForm;
SafeShowFormModal;
FreeForm(frmDesign);
end.
Now when we run the script we'll get a nice output, for every time it says "Timer2 looping!", it says "Timer1 looping!" twice.
Timer1 looping!
Timer1 looping!
Timer2 looping!
Timer1 looping!
Timer1 looping!
Timer2 looping!
Timer1 looping!
Timer1 looping!
Timer2 looping!
That was it, have fun!