Solved

Using TTimer in Threads - OnTimer event never fires.

Posted on 2000-04-25
4
1,062 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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