Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Thread4Unit;
- // oczekiwanie na watek
- interface
- uses System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
- Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Buttons, Vcl.Dialogs;
- type TFindThread = class (TThread)
- protected
- Progr : Integer;
- procedure UpdateProgress;
- procedure Execute; override;
- public
- Found : Integer;
- LookFor : Char;
- Progress : TProgressBar;
- end;
- TMultiFind = class (TThread)
- protected
- Progr : Integer;
- procedure UpdateProgress;
- procedure Execute; override;
- procedure Show;
- public
- LookFor, Output : string;
- Progresses : array [1..5] of TProgressBar;
- end;
- TForm1 = class (TForm)
- Memo1 : TMemo;
- OpenDialog1 : TOpenDialog;
- StaticText1 : TStaticText;
- Edit1 : TEdit;
- BitBtn1 : TBitBtn;
- BitBtn2 : TBitBtn;
- BitBtn3 : TBitBtn;
- ProgressBar1 : TProgressBar;
- ProgressBar2 : TProgressBar;
- ProgressBar3 : TProgressBar;
- ProgressBar4 : TProgressBar;
- ProgressBar5 : TProgressBar;
- procedure EditExit (Sender : TObject);
- procedure StartClick (Sender : TObject);
- procedure LoadClick (Sender : TObject);
- procedure ActivateForm (Sender : TObject);
- end;
- var Form1 : TForm1;
- MainThread : TMultiFind;
- implementation
- {$R *.DFM}
- procedure TFindThread.Execute;
- var i, j : Integer;
- line : string;
- begin
- Found:=0;
- with Form1.Memo1 do
- for i:=0 to Lines.Count-1 do
- begin
- line:=Lines[i];
- for j:=1 to Length(line) do
- if line[j]=LookFor
- then Inc (Found);
- Progr:=i+1;
- Synchronize (UpdateProgress)
- end
- end;
- procedure TFindThread.UpdateProgress;
- begin
- Progress.Position:=Progr
- end;
- procedure TMultiFind.Execute;
- var finders : array [1..4] of TFindThread;
- i : Integer;
- begin
- // ustaw cztery watki
- for i:=1 to 4 do
- begin
- finders[i]:=TFindThread.Create(True);
- finders[i].LookFor:=LookFor[i];
- finders[i].Progress:=Progresses[i+1];
- finders[i].Resume
- end;
- // czekaj az watki sie skoncza
- for i:=1 to 4 do
- begin
- finders[i].WaitFor;
- Progr:=i;
- Synchronize (UpdateProgress)
- end;
- // wyswietl wynik
- Output:='Found: ';
- for i:=1 to 4 do
- Output:=Output+Format('%d %s, ', [finders[i].Found, LookFor[i]]);
- Synchronize (Show);
- // usun watki
- for i:=1 to 4 do
- finders[i].Free
- end;
- procedure TMultiFind.UpdateProgress;
- begin
- // to trzeba napisac
- end;
- procedure TMultiFind.Show;
- begin
- // to trzeba napisac
- end;
- procedure TForm1.EditExit (Sender : TObject);
- begin
- if Length(Edit1.Text)<>4
- then begin
- Edit1.SetFocus;
- ShowMessage ('The edit box requires four characters')
- end
- end;
- procedure TForm1.StartClick (Sender : TObject);
- var i : Integer;
- begin
- if Assigned(MainThread)
- then MainThread.Free;
- MainThread:=TMultiFind.Create(True);
- MainThread.Progresses[1]:=ProgressBar1;
- MainThread.Progresses[2]:=ProgressBar2;
- MainThread.Progresses[3]:=ProgressBar3;
- MainThread.Progresses[4]:=ProgressBar4;
- MainThread.Progresses[5]:=ProgressBar5;
- MainThread.Progresses[1].Max:=4;
- for i:=2 to 5 do
- MainThread.Progresses[i].Max:=Memo1.Lines.Count;
- for i:=1 to 5 do
- MainThread.Progresses[i].Position:=0;
- MainThread.LookFor:=Edit1.text;
- MainThread.Resume
- end;
- procedure TForm1.LoadClick (Sender : TObject);
- begin
- // to trzeba napisac - wczytac zbior z OpenDialog1 do Memo1
- BitBtn1.Enabled:=False;
- BitBtn2.Enabled:=True
- end;
- procedure TForm1.ActivateForm (Sender : TObject);
- begin
- BitBtn1.Enabled:=True;
- BitBtn2.Enabled:=False;
- BitBtn1.SetFocus
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement