Forum > General
TTimer in class does not trigger OnTimer event, why?
hamacker:
I have de code below, it's fail because the event ontimer does not trigger when I started. I dont know why, I see everything OK. There is an expert present to appoint where I fail?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit classe.blinklabel;(*Usage:var BlinkLabel: TBlinkLabel;begin BlinkLabel := TBlinkLabel.Create(Form1.Label1, 5); // Label1 will blink for 5 times BlinkLabel.Start := True;*) {$mode objfpc}{$H+} interface uses Classes, Forms, SysUtils, ExtCtrls, StdCtrls, Graphics; type TBlinkLabel = class(TObject) private FCountLimit: Integer; FMyLabel: TLabel; FStart: Boolean; FMyTimer: TTimer; FCurrentCount: Cardinal; FOldFontStyles: TFontStyles; FOldColor: TColor; FOldMsg: String; procedure SetStart(AValue: Boolean); procedure OnTimer(Sender: TObject); public constructor Create(ALabel: TLabel; ACountLimit: Cardinal); destructor Destroy; override; property CountLimit: Integer read FCountLimit write FCountLimit; property MyLabel: TLabel read FMyLabel write FMyLabel; property Start: Boolean read FStart write SetStart; end; implementation constructor TBlinkLabel.Create(ALabel: TLabel; ACountLimit: Cardinal);begin inherited Create; FMyLabel := ALabel; FCountLimit := ACountLimit; FCurrentCount := 0; FOldFontStyles := ALabel.Font.Style; FOldColor := ALabel.Color; FOldMsg := ALabel.Caption; // Creates and configures the Timer FMyTimer := TTimer.Create(nil); FMyTimer.Interval := 2000; // 2-second interval FMyTimer.OnTimer := @OnTimer; FMyTimer.Enabled := False;end; destructor TBlinkLabel.Destroy;begin FMyTimer.Free; inherited Destroy;end; procedure TBlinkLabel.SetStart(AValue: Boolean);begin if FStart = AValue then Exit; FStart := AValue; if FStart then begin FCurrentCount := 0; FMyTimer.Enabled := True; end else begin FMyTimer.Enabled := False; // Returns to the initial state FMyLabel.Font.Style := FOldFontStyles; FMyLabel.Color := FOldColor; FMyLabel.Caption := FOldMsg; Self.Free; // Class self-destruction end;end; procedure TBlinkLabel.OnTimer(Sender: TObject);begin FMyTimer.Enabled := False; Inc(FCurrentCount); // Toggles the font style between normal and bold if FMyLabel.Caption = '' then FMyLabel.Caption := FOldMsg else FMyLabel.Caption := ''; FMyLabel.Refresh; Beep; // Debug purposes, if it reaches here, you will hear a beep // Checks if the specified seconds have passed if FCurrentCount >= FCountLimit then begin FMyLabel.Font.Style := FOldFontStyles; // Restores the font to the initial state FMyLabel.Color := FOldColor; FMyLabel.Caption := FOldMsg; Self.Free; // Class self-destruction end else begin Application.ProcessMessages; FMyTimer.Enabled := True; end;end; end.
iLya2IK:
Hello. Try to change this line
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FMyTimer := TTimer.Create(nil);// toFMyTimer := TTimer.Create(ALabel.Owner);
Thaddy:
start with unit classe.blinklabel; that is delphi code, not fpc.(yet)
The previous answer can also help you, but just if you are compiling for windows, which you do not mention.
hamacker:
I try Aplication.MainForm too, but still the same.
It's a Windows Application. Lazarus 3.4.
--- Quote from: iLya2IK on September 05, 2024, 03:43:20 pm ---Hello. Try to change this line
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FMyTimer := TTimer.Create(nil);// toFMyTimer := TTimer.Create(ALabel.Owner);
--- End quote ---
alpine:
Specifying the Owner won't help much. Most probably you're blocking the message processing somewhere. I see inappropriate Application.ProcessMessages at line 112. Not sure it is a problem, though.
Can you give a complete minimal project which shows the issue?
Navigation
[0] Message Index
[#] Next page