Solved

Using TTimer in Threads - OnTimer event never fires.

Posted on 2000-04-25
4
1,075 Views
Last Modified: 2012-06-22
I have created by own thread object by deriving a class from TThread and overrided the Execute method with the code to execute.  As part of the thread code, I want to dynamically create a timer and fire a procedure I have written within the thread (the OnTimer event).  However, the OnTimer event never fires.  How to I get it to work?  This is the code I have written...

timMyTimer: TTimer; // This goes in private section of TMyThread class

This is how I've created the timer at the beginning of the execute method...

timMyTimer := TTimer.Create(Main);
timMyTimer.OnTimer := OnTimerEvent;
timMyTimer.Interval :=1000;
timMyTimer.Enabled := True;

procedure TMyThread.OnTimerEvent(Sender: TObject);
begin
  // This should fire every second, but doesn't!
end;

Thanks

Richard2000
0
Comment
Question by:Richard2000
  • 2
4 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2747861
are you sure your thread is running?

0
 
LVL 3

Expert Comment

by:f15iaf
ID: 2748035
Ttimer class is a thread object and i don't sure if you can run thread objects one inside another.
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 50 total points
ID: 2748132
type
  TTimerThread=class(TThread)
  timer:TTimer;
  procedure Execute;override;
  Procedure TimerEvent(Sender:Tobject);
  end;



type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  timethread:TTimerThread;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timethread:=TTimerThread.Create(false);
end;

{ TTimerThread }

procedure TTimerThread.Execute;
begin
   timer:=TTimer.Create(form1);
   Timer.interval:=1000;
   timer.ontimer:=TimerEvent;
   timer.enabled:=true;
   while not terminated do
   begin
     application.processmessages;
   end;
end;

procedure TTimerThread.TimerEvent(Sender: Tobject);
begin
   Form1.Label1.caption:=timeTostr(now);
end;

end.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2748162
unit Unit2;

interface

uses
  Classes,extctrls,Dialogs;

type
  TTest = class(TThread)
  private
    { Private declarations }
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  protected
    procedure Execute; override;
    procedure ShowOK;
  public
    constructor Create(CreateSuspended: Boolean);
    destructor  Destroy;override;
  end;

implementation

{ Test }

constructor TTest.Create(CreateSuspended: Boolean);
begin
  inherited;
  Timer:=TTimer.Create(nil);
  Timer.Interval:=1000;
  Timer.OnTimer:=TimerEvent;
end;

destructor TTest.Destroy;
begin
  Timer.Free;
  inherited;
end;

procedure TTest.Execute;
begin
  while Terminated do ;
end;

procedure TTest.ShowOK;
begin
  ShowMessage('Ok');
end;

procedure TTest.TimerEvent(Sender: TObject);
begin
  Timer.Enabled:=False;
  Synchronize(ShowOK);
  Timer.Enabled:=True;
end;

end.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question