Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.32 KB | None | 0 0
  1. unit Thread4Unit;
  2. // oczekiwanie na watek
  3. interface
  4. uses System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
  5.      Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Buttons, Vcl.Dialogs;
  6. type TFindThread = class (TThread)
  7.                      protected
  8.                        Progr : Integer;
  9.                        procedure UpdateProgress;
  10.                        procedure Execute; override;
  11.                      public
  12.                        Found : Integer;
  13.                        LookFor : Char;
  14.                        Progress : TProgressBar;
  15.                    end;
  16.      TMultiFind  = class (TThread)
  17.                      protected
  18.                        Progr : Integer;
  19.                        procedure UpdateProgress;
  20.                        procedure Execute; override;
  21.                        procedure Show;
  22.                      public
  23.                        LookFor, Output : string;
  24.                        Progresses      : array [1..5] of TProgressBar;
  25.                    end;
  26.      TForm1      = class (TForm)
  27.                      Memo1        : TMemo;
  28.                      OpenDialog1  : TOpenDialog;
  29.                      StaticText1  : TStaticText;
  30.                      Edit1        : TEdit;
  31.                      BitBtn1      : TBitBtn;
  32.                      BitBtn2      : TBitBtn;
  33.                      BitBtn3      : TBitBtn;
  34.                      ProgressBar1 : TProgressBar;
  35.                      ProgressBar2 : TProgressBar;
  36.                      ProgressBar3 : TProgressBar;
  37.                      ProgressBar4 : TProgressBar;
  38.                      ProgressBar5 : TProgressBar;
  39.                      procedure EditExit (Sender : TObject);
  40.                      procedure StartClick (Sender : TObject);
  41.                      procedure LoadClick (Sender : TObject);
  42.                      procedure ActivateForm (Sender : TObject);
  43.                    end;
  44. var Form1      : TForm1;
  45.     MainThread : TMultiFind;
  46. implementation
  47. {$R *.DFM}
  48. procedure TFindThread.Execute;
  49. var i, j : Integer;
  50.     line : string;
  51. begin
  52.   Found:=0;
  53.   with Form1.Memo1 do
  54.     for i:=0 to Lines.Count-1 do
  55.       begin
  56.         line:=Lines[i];
  57.         for j:=1 to Length(line) do
  58.           if line[j]=LookFor
  59.             then Inc (Found);
  60.         Progr:=i+1;
  61.         Synchronize (UpdateProgress)
  62.       end
  63. end;
  64. procedure TFindThread.UpdateProgress;
  65. begin
  66.   Progress.Position:=Progr
  67. end;
  68. procedure TMultiFind.Execute;
  69. var finders : array [1..4] of TFindThread;
  70.     i       : Integer;
  71. begin
  72.   // ustaw cztery watki
  73.   for i:=1 to 4 do
  74.     begin
  75.       finders[i]:=TFindThread.Create(True);
  76.       finders[i].LookFor:=LookFor[i];
  77.       finders[i].Progress:=Progresses[i+1];
  78.       finders[i].Resume
  79.     end;
  80.   // czekaj az watki sie skoncza
  81.   for i:=1 to 4 do
  82.     begin
  83.       finders[i].WaitFor;
  84.       Progr:=i;
  85.       Synchronize (UpdateProgress)
  86.     end;
  87.   // wyswietl wynik
  88.   Output:='Found: ';
  89.   for i:=1 to 4 do
  90.     Output:=Output+Format('%d %s, ', [finders[i].Found, LookFor[i]]);
  91.   Synchronize (Show);
  92.   // usun watki
  93.   for i:=1 to 4 do
  94.     finders[i].Free
  95. end;
  96. procedure TMultiFind.UpdateProgress;
  97. begin
  98. // to trzeba napisac
  99. end;
  100. procedure TMultiFind.Show;
  101. begin
  102. // to trzeba napisac
  103. end;
  104. procedure TForm1.EditExit (Sender : TObject);
  105. begin
  106.   if Length(Edit1.Text)<>4
  107.     then begin
  108.            Edit1.SetFocus;
  109.            ShowMessage ('The edit box requires four characters')
  110.          end
  111. end;
  112. procedure TForm1.StartClick (Sender : TObject);
  113. var i : Integer;
  114. begin
  115.   if Assigned(MainThread)
  116.     then MainThread.Free;
  117.   MainThread:=TMultiFind.Create(True);
  118.   MainThread.Progresses[1]:=ProgressBar1;
  119.   MainThread.Progresses[2]:=ProgressBar2;
  120.   MainThread.Progresses[3]:=ProgressBar3;
  121.   MainThread.Progresses[4]:=ProgressBar4;
  122.   MainThread.Progresses[5]:=ProgressBar5;
  123.   MainThread.Progresses[1].Max:=4;
  124.   for i:=2 to 5 do
  125.     MainThread.Progresses[i].Max:=Memo1.Lines.Count;
  126.   for i:=1 to 5 do
  127.     MainThread.Progresses[i].Position:=0;
  128.   MainThread.LookFor:=Edit1.text;
  129.   MainThread.Resume
  130. end;
  131. procedure TForm1.LoadClick (Sender : TObject);
  132. begin
  133.   // to trzeba napisac - wczytac zbior z OpenDialog1 do Memo1
  134.   BitBtn1.Enabled:=False;
  135.   BitBtn2.Enabled:=True
  136. end;
  137. procedure TForm1.ActivateForm (Sender : TObject);
  138. begin
  139.   BitBtn1.Enabled:=True;
  140.   BitBtn2.Enabled:=False;
  141.   BitBtn1.SetFocus
  142. end;
  143. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement