hi everybody,
i'm going to set the procedure for OnTimer event runtime but i don't know how...
i've written the following (WRONG) program, can someone help me?

var
Timer1: TTimer;

procedure PROC(Sender: TObject);
begin .. end;

begin
Timer1.Create(Timer1) ;
Timer1.Interval := 2000;
Timer1.OnTimer := PROC;
end;

Member Avatar for Micheus

i'm going to set the procedure for OnTimer event runtime but i don't know how...
i've written the following (WRONG) program, can someone help me?

mfran2002, the OnTimer event expect that a TNotifyEvent be assigned to it.
see, from the help:

type TNotifyEvent = procedure (Sender: TObject) of object;

think it like a procedure of a object. So, your procedure must be declared into your form class definition, on this case:

...
type
  TMainForm = class(TForm)
    ...
  public
    procedure OnTimeEvent(Sender :TObject);
  end;

var
  MainForm :TMainForm;

implementation
...
procedure TMainForm.OnTimeEvent(Sender :TObject);
begin
  ...  // your code here
end;
...

Now, You must adjust your code above:

var
  Timer1: TTimer;
begin
  Timer1.Create(nil);
  Timer1.Interval := 2000;
  Timer1.OnTimer := MainForm.OnTimerEvent;
end;

observe that parameter passed to Create methode from TTimer must be the Owner them. I suggest you to pass nil (no owner) or MainForm - it's more prudent.

Bye

thanks a lot Micheus, very interesting.
however, for this project, i've not any form, i do use only program source code, like this:

program myprg;
begin
...
end.


in thi case how can i modify your suggestion?

thanks

Maybe i've found the solution to my problem using API function SetTimer with TimerProc CALLBACK but i do not know how i can use it...i need an example to follow...

UINT SetTimer(
HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procedure
);

VOID CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
);

to execute every 2000 msec my procedure TMyTimer.TimerEvent(Sender:TObject)

i'm trying to use this statement:
SetTimer(0, 0, 2000, addr(Timer1.TimerEvent(Timer1)));

but i get this error:
VARIABLE REQUIRED

???!?!?!?
where? when? why?
AAAAAHHHHHHHHHHHHHHHH
help me please!!!!

Member Avatar for Micheus

however, for this project, i've not any form, i do use only program source code

mfran2002, You can use SetTimer API function to do this and pass 0 (null) to function in hwnd param.

But is necessary that You use the GetMessage and DispatchMessage API's functions in order to keep message windows system unchanged.

You can test this sample:

program AppTimer;
{$APPTYPE CONSOLE}
uses
  Dialogs,
  Windows, WinTypes;

var
  Counter :Integer;

// callback function for Timer
procedure TimerProc(wnd :HWND;	    // handle of window for timer messages
                    uMsg :UINT;	  // WM_TIMER message
                    idEvent :UINT;	// timer identifier
                    dwTime :DWORD 	// current system time
                    ); stdcall;  // use stdcall when declare callback functions
begin
  Inc(Counter);
  WriteLn('OnTimer passed...', Counter);
end;

var
  Number :Integer;
  TimeHandle :UINT;
  Msg :TMSG;
begin
  Counter := 0;
  TimeHandle := SetTimer(0, 0, 1000, @TimerProc);
  if TimeHandle > 0 then
  try
    while (Counter < 10) do
    begin
      GetMessage(Msg, 0, 0, 0);
      DispatchMessage(Msg);
     // uncomment this code portion and see the result
     // Write('Input some number: '); Readln(Number);
    end;
  finally
    KillTimer(0, TimeHandle);
  end;
end.

I hope this help you.

Micheus

I L-O-V-E Y-O-U !!!!!

you saved my life....is it enaugh?
;))

the last question: is it possible to launch console application like an icon on taskbar? or iconized?

thanks a lot!!!!

Member Avatar for Micheus

the last question: is it possible to launch console application like an icon on taskbar? or iconized?

If you omit {$APPTYPE CONSOLE} your application will run in "hidden mode".

Put a icon in systray will exige a window handle to interaction with your application (I think). Is possoble create a invisible window to do this but I haven't a sample to you. Search for AllocateHWnd and Shell_NotifyIcon.

Bye

thanks a lot
again

Meby this way...

var
Timer1: TTimer;

procedure TForm1.PROC(Sender: TObject);
begin .. end;

begin
Timer1 := TTimer.Create(NIL) ;
Timer1.Interval := 2000;
Timer1.OnTimer := PROC;
Timer1.Enabled := True;
end;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.