unit Main;

interface

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

type
  TMainForm = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    btnSTART: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure btnSTARTClick(Sender: TObject);

  private
	 ilosc_watkow : Integer;
    my_thread1          : MWatek;
    my_thread2          : MWatek;
    my_thread3          : MWatek;

  public
	 procedure MyOnTerminate(Sender : TObject);
  end;

var
  MainForm: TMainForm;

implementation
{$R *.dfm}
{------------------------------------------------------------------------------}
procedure TMainForm.FormShow(Sender: TObject);
begin
   Memo1.Clear();
   Memo1.Align := alLeft;
end;

procedure TMainForm.MyOnTerminate(Sender: TObject);

begin
	Dec(ilosc_watkow);
   Memo1.Lines.Add('Ilość działających wątków: ' + IntToStr(ilosc_watkow));
end;

procedure TMainForm.btnSTARTClick(Sender: TObject);
begin
	ilosc_watkow := 0;

   Memo1.Clear();

   if btnSTART.Caption = '&Start' then
   begin
   	btnSTART.Caption := '&Koniec';

      my_thread1 := mWatek.Create(true, Memo1, 1);
      my_thread1.OnTerminate := MyOnTerminate;
      Inc(ilosc_watkow);

      my_thread2 := mWatek.Create(true, Memo1, 2);
      my_thread2.OnTerminate := MyOnTerminate;
      Inc(ilosc_watkow);

      my_thread3 := mWatek.Create(true, Memo1, 3);
      my_thread3.OnTerminate := MyOnTerminate;
      Inc(ilosc_watkow);

      my_thread1.Priority := tpLowest;
      my_thread1.Resume();

      my_thread2.Priority := tpNormal;
      my_thread2.Resume();

      my_thread3.Priority := tpHighest;
      my_thread3.Resume();
   end
   else begin
      Application.Terminate();
   end;
end;

end.

