No form TTimer - Delphi

This is a discussion on No form TTimer - Delphi ; I'm running in to a problem that makes me loss my hair. I'm trying to create a timer in Unit (No Form) and I getting the following errors: Here is my code: procedure Start; var Timer1: TTimer; begin Timer1 := ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

No form TTimer

  1. Default No form TTimer

    I'm running in to a problem that makes me loss my hair.
    I'm trying to create a timer in Unit (No Form) and I getting the following
    errors: Here is my code:

    procedure Start;
    var
    Timer1: TTimer;
    begin
    Timer1 := TTimer.Create(self); "Undeclared identifier: 'self '"
    Timer1.OnTimer := Timer1Timer(self); " Incompatible types:
    'TNotifyEvent' and 'Procedure, untyped pointer or untyped parameter'
    Timer1.Interval := 250;
    Timer1.Enabled := True;
    end;

    procedure Timer1Timer(Sender: TObject);
    begin
    Timer1.Enabled := False; " Undeclared identifier: Timer1'"
    if FileExists('C:\Test.txt') then
    Timer1.Enabled := True;
    end;

    Any help, thanks.
    Kim



  2. Default Re: No form TTimer



    ----------
    In article <47abbf26$1@newsgroups.borland.com>, "Kim Jensen"
    <kim@comcasystems.com> wrote:


    > I'm running in to a problem that makes me loss my hair.
    > I'm trying to create a timer in Unit (No Form) and I getting the following
    > errors: Here is my code:
    >
    > procedure Start;
    > var
    > Timer1: TTimer;
    > begin
    > Timer1 := TTimer.Create(self); "Undeclared identifier: 'self '"

    Self refers to the object instance that this is a method of.
    But this is not a method at all. It is an ordinary procedure.
    A method has a name like
    TMyClass.Start;

    But why do you want the non-existent 'self' to own the timer, anyway.
    You could fix this one by using Create(nil);
    > Timer1.OnTimer := Timer1Timer(self); " Incompatible types:
    > 'TNotifyEvent' and 'Procedure, untyped pointer or untyped parameter'

    An OnTimer handler has to be a method.
    For the same reasons as above, your Timer1Timer is a procedure not a method.
    > Timer1.Interval := 250;
    > Timer1.Enabled := True;
    > end;
    >
    > procedure Timer1Timer(Sender: TObject);
    > begin
    > Timer1.Enabled := False; " Undeclared identifier: Timer1'"

    There is no variable called Timer1 visible here.
    > if FileExists('C:\Test.txt') then
    > Timer1.Enabled := True;
    > end;
    >
    > Any help, thanks.


    interface

    type TMyClass=Class(TObject)
    private
    Timer1:TTimer;
    procedure Timer1Timer(Sender:TObject);
    public
    Procedure Start;
    end;

    implementation

    procedure TmyClass.Start;
    begin
    Timer1 := TTimer.Create(self); // self means the current Tmyclass
    Timer1.OnTimer := Timer1Timer(self); // means the private method of
    TmyClass
    Timer1.Interval := 250;
    Timer1.Enabled := True;
    end;

    procedure TMyClass.Timer1Timer(Sender: TObject);
    begin
    Timer1.Enabled := False; // Timer1 can be seen, its the private field of
    the current TMyClass, aka self
    if FileExists('C:\Test.txt') then
    Timer1.Enabled := True;
    end;

    ----

    Don't forget to Create a TmyClass before you tell it to Start!

  3. Default Re: No form TTimer

    > type TMyClass=Class(TObject)

    [...]

    > procedure TmyClass.Start;
    > begin
    > Timer1 := TTimer.Create(self); // self means the current Tmyclass


    TTimer.Create needs an object reference from TComponent (or derived from
    TComponent), so the code above won't compile either (but the error
    message is different).

    But AFAIK you can also create a TTimer passing nil to the constructor
    :-) (just make sure you don't forget to free it manually in this case)

    --
    Jens Gruschel
    http://www.pegtop.net

  4. Default Re: No form TTimer


    "Kim Jensen" <kim@comcasystems.com> wrote in message
    news:47abbf26$1@newsgroups.borland.com...

    > Timer1 := TTimer.Create(self); "Undeclared identifier: 'self '"


    Since Start() is not a member of a class, there is no Self pointer
    available. You will have to nil as the Owner instead:

    Timer1 := TTimer.Create(nil);

    By using a nil Owner, you are responsible for freeing the TTimer yourself
    when you are done using it. The problem, however, is that you are storing
    the TTimer instance into a local variable that will no longer exist once
    Start() has exited. So you will have to use a global variable instead of a
    local variable so that you can access the TTimer object when needed.

    > Timer1.OnTimer := Timer1Timer(self); " Incompatible types:


    You are trying to actually call Timer1Timer() and then assign its return
    value to the event. You need to remove the '(self)' portion from the code
    in order to assign the memory address of Timer1Timer() to the event instead:

    Timer1.OnTimer := Timer1Timer;

    Note that Timer1Timer() must be a member of a class in order to be used as
    an event handler. Given the code you have shown, you should move the TTimer
    into a wrapper class of its own, ie:

    type
    TMyTimer = class
    private
    fTimer: TTimer;
    procedure TimerElapsed(Sender: TObject);
    public
    constructor Create;
    destructor Destroy; override;
    end;

    var
    gTimer: TMyTimer = nil;

    constructor TMyTimer.Create;
    begin
    inherited;
    fTimer := TTimer.Create(nil);
    fTimer.OnTimer := TimerElapsed;
    fTimer.Interval := 250;
    fTimer.Enabled := True;
    end;

    destructor TMyTimer.Destroy;
    begin
    fTimer.Free;
    inherited;
    end;

    procedure TMyTimer.TimerElapsed(Sender: TObject);
    begin
    fTimer.Enabled := FileExists('C:\Test.txt');
    end;

    procedure Start;
    begin
    Stop;
    gTimer := TMyTimer.Create;
    end;


    procedure Stop;
    begin
    FreeAndNil(gTimer);
    end;

    The alternative is to not use a TTimer at all. You can call the Win32 API
    SetTimer() function directly instead, passing it a non-member callback
    function, ie:

    var
    gTimer: UINT = 0;

    procedure TimerElapsed(Wnd: HWND; uMsg, idEvent: UINT; dwTime: DWORD);
    stdcall;
    begin
    if not FileExists('C:\Test.txt') then
    Stop;
    end;

    procedure Start;
    begin
    Stop;
    gTimer := Windows.SetTimer(0, 0, 250, @TimerElapsed);
    end;

    procedure Stop;
    begin
    if gTimer <> 0 then
    begin
    Windows.KillTimer(0, gTimer);
    gTimer := 0;
    end;
    end;


    Gambit



  5. Default Re: No form TTimer

    Thanks.
    I like the direct call to the API, but I must be missing a Unit in my uses
    because I get an "Undeclared identifier: UINT

    Kim

    > The alternative is to not use a TTimer at all. You can call the Win32 API
    > SetTimer() function directly instead, passing it a non-member callback
    > function, ie:
    >
    > var
    > gTimer: UINT = 0;
    >
    > procedure TimerElapsed(Wnd: HWND; uMsg, idEvent: UINT; dwTime: DWORD);
    > stdcall;
    > begin
    > if not FileExists('C:\Test.txt') then
    > Stop;
    > end;
    >
    > procedure Start;
    > begin
    > Stop;
    > gTimer := Windows.SetTimer(0, 0, 250, @TimerElapsed);
    > end;
    >
    > procedure Stop;
    > begin
    > if gTimer <> 0 then
    > begin
    > Windows.KillTimer(0, gTimer);
    > gTimer := 0;
    > end;
    > end;
    >
    >
    > Gambit
    >




  6. Default Re: No form TTimer


    "Kim Jensen" <kim@comcasystems.com> wrote in message
    news:47acfe77$1@newsgroups.borland.com...

    > I like the direct call to the API, but I must be missing a Unit
    > in my uses because I get an "Undeclared identifier: UINT


    UINT is declared in the 'Windows' unit.


    Gambit



  7. Default Re: No form TTimer

    Kim Jensen wrote:

    > I like the direct call to the API, but I must be missing a Unit in my
    > uses because I get an "Undeclared identifier: UINT


    It is declared in the unit Windows, make sure it is in your *uses*.

    --
    Pieter

  8. Default Re: No form TTimer


    "Kim Jensen" wrote
    > I'm running in to a problem that makes me loss my hair.
    > I'm trying to create a timer in Unit (No Form)

    ......................
    > Any help, thanks.
    > Kim


    Here is an extract from one of my units:

    unit MyControl.
    Interface
    uses .........;
    type

    TFunctions = class
    class procedure _Idle(Sender: TObject);
    end;

    TControl = record
    private
    .........
    public
    constructor create(.....);
    ...........
    end;
    ................
    var
    D_Ctrl : TControl;
    D_Timer: TTimer;
    .................
    implementation
    uses ..........;

    class procedure _Idle(Sender: TObject);
    var
    ..........
    begin
    ...........
    end;

    constructor TControl.create(.......);
    begin
    ........
    D_Timer := TTimer.Create(nil);
    D_Timer.Interval := 10;
    D_Timer.OnTimer := TFunctions._Idle;
    D_Timer.Enabled := false; // D_Timer is started and stopped from other
    code
    end;
    ..............

    initialization;
    //--------------------------------------------------
    finalization
    begin
    FreeAndNil(D_Timer);
    end;

    end.


    Hope this will help

    regards Sven



  9. Default Re: No form TTimer

    I think I'm having a brain fart. When I tried to implement the code below
    inside a component of mine I get a Variable required error.


    Here is where I get the error:

    {------------------------------------------------------------------------------}
    procedure TTransport.StartOutgoingDataPacketToSendTimer(TimerTime: UINT);
    begin

    StopOutgoingDataPacketToSendTimer;
    OutGoingDataPacketToSendTimer :=
    Windows.SetTimer(0, 0, 1000, @OutGoingDataPacketToSendTrigger);
    <====Error at the end of this line

    end;

    What am I doing wrong?


    Thanks in advance,
    Rodger Van Kirk



    "Kim Jensen" <kim@comcasystems.com> wrote in message
    news:47acfe77$1@newsgroups.borland.com...
    > Thanks.
    > I like the direct call to the API, but I must be missing a Unit in my uses
    > because I get an "Undeclared identifier: UINT
    >
    > Kim
    >
    >> The alternative is to not use a TTimer at all. You can call the Win32
    >> API SetTimer() function directly instead, passing it a non-member
    >> callback function, ie:
    >>
    >> var
    >> gTimer: UINT = 0;
    >>
    >> procedure TimerElapsed(Wnd: HWND; uMsg, idEvent: UINT; dwTime: DWORD);
    >> stdcall;
    >> begin
    >> if not FileExists('C:\Test.txt') then
    >> Stop;
    >> end;
    >>
    >> procedure Start;
    >> begin
    >> Stop;
    >> gTimer := Windows.SetTimer(0, 0, 250, @TimerElapsed);
    >> end;
    >>
    >> procedure Stop;
    >> begin
    >> if gTimer <> 0 then
    >> begin
    >> Windows.KillTimer(0, gTimer);
    >> gTimer := 0;
    >> end;
    >> end;
    >>
    >>
    >> Gambit
    >>

    >
    >



  10. Default Re: No form TTimer

    Rodger Van Kirk wrote:

    > I think I'm having a brain fart. When I tried to implement the code
    > below inside a component of mine I get a Variable required error.
    >
    >
    > Here is where I get the error:
    >
    > {---------------------------------------------------------------------
    > ---------} procedure
    > TTransport.StartOutgoingDataPacketToSendTimer(TimerTime: UINT); begin
    >
    > StopOutgoingDataPacketToSendTimer;
    > OutGoingDataPacketToSendTimer :=
    > Windows.SetTimer(0, 0, 1000, @OutGoingDataPacketToSendTrigger);
    > <====Error at the end of this line
    >
    > end;
    >
    > What am I doing wrong?


    How is OutGoingDataPacketToSendTrigger defined? Note that it cannot be
    a method of some class, it has to be a standalone function.


    --
    Peter Below (TeamB)
    Don't be a vampire (http://slash7.com/pages/vampires),
    use the newsgroup archives :
    http://www.tamaracka.com/search.htm
    http://groups.google.com

+ Reply to Thread
Page 1 of 2 1 2 LastLast