{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
unit uMain;

interface

{$IFDEF KOL_MCK}
uses Windows, Messages, KOL {$IFNDEF KOL_MCK},
 mirror, Classes, Controls, mckCtrls, mckObjs, Graphics,
 {$ENDIF (place your units here->)}, KolThdTimer;
{$ELSE}
{$I uses.inc}
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, mirror;
{$ENDIF}

type
  {$IFDEF KOL_MCK}
  {$I MCKfakeClasses.inc}
  {$IFDEF KOLCLASSES} {$I TMainFormclass.inc} {$ELSE OBJECTS} PMainForm = ^TMainForm; {$ENDIF CLASSES/OBJECTS}
  {$IFDEF KOLCLASSES}{$I TMainForm.inc}{$ELSE} TMainForm = object(TObj) {$ENDIF}
    Form: PControl;
  {$ELSE not_KOL_MCK}
  TMainForm = class(TForm)
  {$ENDIF KOL_MCK}
    KOLProjectDemo: TKOLProject;
    KOLForm1: TKOLForm;
    Button1: TKOLButton;
    CheckBox1: TKOLCheckBox;
    CheckBox2: TKOLCheckBox;
    Timer2: TKOLTimer;
    ProgressBar1: TKOLProgressBar;
    ProgressBar2: TKOLProgressBar;
    procedure KOLForm1FormCreate(Sender: PObj);
    procedure CheckBox1Click(Sender: PObj);
    procedure CheckBox2Click(Sender: PObj);
    procedure Timer2Timer(Sender: PObj);
    procedure KOLForm1Destroy(Sender: PObj);
    procedure Button1Click(Sender: PObj);
    procedure KOLForm1Close(Sender: PObj; var Accept: Boolean);
  private
    { Private declarations }
    ThreadTimer: PKolThreadedTimer;
    procedure InternalOnTimer(Sender: PObj);
  public
    { Public declarations }
  end;

var
  MainForm {$IFDEF KOL_MCK} : PMainForm {$ELSE} : TMainForm {$ENDIF} ;

{$IFDEF KOL_MCK}
procedure NewMainForm( var Result: PMainForm; AParent: PControl );
{$ENDIF}

implementation

{$IFNDEF KOL_MCK} {$R *.DFM} {$ENDIF}


{$IFDEF KOL_MCK}
{$I uMain_1.inc}
{$ENDIF}

procedure TMainForm.InternalOnTimer(Sender: PObj);
begin
//The use of property KeepAlive + Enabled/Disabled
  with ThreadTimer^ do
  begin
   Enabled := False;
   if ProgressBar1.Progress = 100 then
   begin
    ProgressBar1.Progress := 0;
     CheckBox1.Checked:= False;
      KeepAlive:= False;
       Exit;
   end;
   //Just playing with the progress bar while Enabled + KeepAlive
   if (ProgressBar1.Progress + 1) <= ProgressBar1.MaxProgress then
       ProgressBar1.Progress := ProgressBar1.Progress + 1
    else ProgressBar1.Progress := 0;
   Enabled := True;
  end;
end;


procedure TMainForm.KOLForm1FormCreate(Sender: PObj);
begin
   ThreadTimer:= NewKolThreadedTimer;
  with ThreadTimer^ do
  begin
   OnTimer := InternalOnTimer;
    Interval:= 200;
     Enabled := False;
      KeepAlive := True; //True - Not to Terminate,
                        // False - Terminate thread of timer if Enabled = False
     ThreadPriority:= THREAD_PRIORITY_IDLE; //Low priority of thread
  end;
end;

procedure TMainForm.CheckBox1Click(Sender: PObj);
begin
  ThreadTimer.Enabled := CheckBox1.Checked;
   if CheckBox1.Checked then ThreadTimer.KeepAlive:= True;
     if not ThreadTimer.Enabled then ProgressBar1.Progress := 0;
end;

procedure TMainForm.CheckBox2Click(Sender: PObj);
begin
 Timer2.Enabled:= CheckBox2.Checked;
  if not Timer2.Enabled then ProgressBar2.Progress := 0;
end;

procedure TMainForm.Timer2Timer(Sender: PObj);
begin
  if (ProgressBar2.Progress + 1) <= ProgressBar2.MaxProgress then
    ProgressBar2.Progress := ProgressBar2.Progress + 1
  else
    ProgressBar2.Progress := 0;
end;

procedure TMainForm.KOLForm1Destroy(Sender: PObj);
begin
 if Assigned(ThreadTimer) then
  ThreadTimer.Free;
end;

procedure TMainForm.Button1Click(Sender: PObj);
begin
 Form.Close;
end;

procedure TMainForm.KOLForm1Close(Sender: PObj; var Accept: Boolean);
begin
 Timer2.Enabled:= False;
end;

end.
















