home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / speed / UnitStartTwoThreads.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-05-18  |  2.8 KB  |  105 lines

  1. unit UnitStartTwoThreads;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, Grids, DBGrids, DB;
  8.  
  9. type
  10.   TFrTwoThreads = class(TForm)
  11.     Panel1: TPanel;
  12.     Memo1: TMemo;
  13.     Memo2: TMemo;
  14.     BuClose: TButton;
  15.     BuStartTwoThreads: TButton;
  16.     DBGrid1: TDBGrid;
  17.     DBGrid2: TDBGrid;
  18.     procedure BuStartTwoThreadsClick(Sender: TObject);
  19.     procedure FormResize(Sender: TObject);
  20.     procedure FormDestroy(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   FrTwoThreads: TFrTwoThreads;
  30.  
  31. implementation
  32.  
  33. uses UnitThreads, UnitDataModule;
  34.  
  35. var
  36.    Thread1,Thread2: TThreadedQuery;
  37.  
  38. {$R *.DFM}
  39.  
  40. procedure TFrTwoThreads.BuStartTwoThreadsClick(Sender: TObject);
  41. var
  42.    Alias,UserName,Password:           string;
  43.    FirstQuery, SecondQuery:           TStrings;
  44.  
  45. begin
  46.      // If Threads already exist then free
  47.      Thread1.Free;
  48.      Thread2.Free;
  49.      Thread1:= nil;
  50.      Thread2:= nil;
  51.      MessageDlg ('About to start two threads to perform to separate queries.',
  52.                   mtInformation, [mbOK,mbCancel], 0);
  53.      Alias:= DataModule2.Database1.AliasName;
  54.      UserName:= DataModule2.Database1.Params.Values['USER NAME'];
  55.      Password:= DataModule2.Database1.Params.Values['PASSWORD'];
  56.      FirstQuery:= TStringList.Create;
  57.      SecondQuery:= TStringList.Create;
  58.      try
  59.         FirstQuery.Assign(Memo1.Lines);
  60.         SecondQuery.Assign(Memo2.Lines);
  61.         Thread1:= TThreadedQuery.Create ('One',Alias,UserName,Password,FirstQuery,DBGrid1);
  62.         Thread2:= TThreadedQuery.Create ('Two',Alias,UserName,Password,SecondQuery,DBGrid2);
  63.         //Note the Thread is freed when the this form is destroyed
  64.      finally
  65.             FirstQuery.Free;
  66.             SecondQuery.Free;
  67.      end;
  68. end;
  69.  
  70. procedure TFrTwoThreads.FormResize(Sender: TObject);
  71. var
  72.    MainFormWidth,
  73.    MainFormWidthDiv2: integer;
  74. begin
  75.      MainFormWidth:= FrTwoThreads.Width;
  76.      MainFormWidthDiv2:= MainFormWidth div 2;
  77.      Memo1.Width:=       MainFormWidthDiv2 - 20;
  78.      Memo2.Width:=       MainFormWidthDiv2 - 20;
  79.      Memo1.Left:=        1;
  80.      Memo2.Left:=        MainFormWidthDiv2 + 10;
  81.      DBGrid1.Width:=     MainFormWidthDiv2-1;
  82.      DBGrid1.Left:=      1;
  83.      DBGrid2.Width:=     MainFormWidthDiv2-1;
  84.      DBGrid2.Left:=      MainFormWidthDiv2+1;
  85.      BuStartTwoThreads.Left:= Memo1.Left+20;
  86.      BuClose.Left:=           Memo2.Left+20;
  87. end;
  88.  
  89. procedure TFrTwoThreads.FormDestroy(Sender: TObject);
  90. begin
  91.      // If Threads already exist then free
  92.      Thread1.Free;
  93.      Thread2.Free;
  94.      Thread1:= nil;
  95.      Thread2:= nil;
  96. end;
  97.  
  98. procedure TFrTwoThreads.FormCreate(Sender: TObject);
  99. begin
  100.      Panel1.Caption:= '';
  101.  
  102. end;
  103.  
  104. end.
  105.