home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 June / HDC50.iso / Runimage / Delphi50 / Help / Examples / Prgrsbar / PG2.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  2.0 KB  |  76 lines

  1. unit Pg2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, comctrls;
  7.  
  8. type
  9.   TMyThread = class(TThread)
  10.   private
  11.     PB : TProgressBar;   // Reference to ProgressBar
  12.     procedure InitProgressBar; // Setup ProgressBar
  13.     procedure UpdateProgressBar; // Update ProgressBar
  14.   protected
  15.     procedure Execute; override; // Main thread execution
  16.   published
  17.     constructor CreateIt(PriorityLevel: cardinal; ProgBar : TProgressBar);
  18.     destructor Destroy; override;
  19.   end;
  20.  
  21. implementation
  22.  
  23. uses
  24.  windows, Pg1;
  25.  
  26. constructor TMyThread.CreateIt(PriorityLevel: cardinal; ProgBar : TProgressBar);
  27. begin
  28.   inherited Create(true);      // Create thread suspended
  29.   Priority := TThreadPriority(PriorityLevel); // Set Priority Level
  30.   FreeOnTerminate := true; // Thread Free Itself when terminated
  31.   PB := ProgBar;    // Set reference
  32.   Synchronize(InitProgressBar); // Setup the ProgressBar
  33.   Suspended := false;         // Continue the thread
  34. end;
  35.  
  36. destructor TMyThread.Destroy;
  37. begin
  38.    PostMessage(form1.Handle,wm_ThreadDoneMsg,self.ThreadID,0);
  39.    {
  40.      This posts a message to the main form, tells us when and which thread
  41.      is done executing.
  42.    }
  43.    inherited destroy;
  44. end;
  45.  
  46.  
  47. procedure TMyThread.Execute; // Main execution for thread
  48. var
  49.  i : cardinal;
  50. begin
  51.   i := 1;
  52.   while ((Terminated = false) and (i < 100000)) do
  53.   begin
  54.       Synchronize(UpdateProgressBar); // Update ProgressBar, uses sychronize because ProgressBar is in another thread
  55.       Inc(i);
  56.       // if Terminated is true, this loop exits prematurely so the thread will terminate
  57.   end;
  58. end;
  59.  
  60. procedure TMyThread.InitProgressBar; // setup/initialize the ProgressBar
  61. begin
  62.   PB.Min := 1;  // minimum value for bar
  63.   PB.Max := 100000; // maximum value for bar
  64.   PB.Step := 1;   // size will be used by each call to StepIt
  65.   PB.Position := 1; // set position to begining
  66. end;
  67.  
  68.  
  69. procedure TMyThread.UpdateProgressBar; // Updates the ProgressBar
  70. begin
  71.   PB.StepIt; // step the bar
  72. end;
  73.  
  74.  
  75. end.
  76.