Einzelnen Beitrag anzeigen

FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#1

Basic TThread doesn't work

  Alt 1. Feb 2008, 07:55
Ok, I am trying to figure out how this TThread Class work, here is my example:

I am trying to test and see if a thread makes my main application responsive, so that it doesn't seem like it's not responding to events and clicks on the main form, so i created a TThread class, which does a maths calculation, this takes about 8 seconds to complete, giving me time to see if my main app is responsive, but it doesn't seem to work, what I am doing wrong?

My Main Form

Delphi-Quellcode:
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, System.ComponentModel, Borland.Vcl.StdCtrls,Borland.Vcl.ComCtrls, Borland.Vcl.ExtCtrls,
  IdBaseComponent, IdThreadComponent;

type
  TForm3 = class(TForm)
    Button1: TButton;
    MainProgress: TProgressBar;
    IdThreadComponent1: TIdThreadComponent;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
      procedure MyOnTerminate(Sender:TObject);

  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses MyThread;


{$R *.nfm}
//*******************************************************//
procedure TForm3.Button1Click(Sender: TObject);
var
  TT : TMyThread;
begin

  TT := TMyThread.Create(True);
  TT.FreeOnTerminate := True;

  TT.OnTerminate := MyOnTerminate;
  TT.Resume;

end;
//*******************************************************//
procedure TForm3.MyOnTerminate(Sender: TObject);
begin
  ShowMessage('Done');
end;
//*******************************************************//
end.
My Thread Class:

Delphi-Quellcode:
unit MyThread;

interface

uses Classes,SysUtils,Dialogs,dte_Update_Library_DB,ADODB,ComCtrls;

type
   TMyThread = class(TThread)

  private
     procedure DoIt;

  protected
    procedure Execute; override;

end;

implementation

//*******************************************************//
procedure TMyThread.Execute;
begin
  synchronize(DoIt);
end;
//*******************************************************//
procedure TMyThread.DoIt;
var
  Count : double;
  i : integer;
begin
  for i := 0 to 900000000000000000 - 1 do
  begin
    Count := Count + i -(i * 2) + i;

  end;

  Terminate;
end;
//*******************************************************//

end.
  Mit Zitat antworten Zitat