Solved

Using TTimer in Threads - OnTimer event never fires.

Posted on 2000-04-25
4
1,047 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
Comment Utility
are you sure your thread is running?

0
 
LVL 3

Expert Comment

by:f15iaf
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now