User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 429,898 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,351 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1856 | Replies: 8
Reply
Join Date: Mar 2008
Posts: 7
Reputation: mfran2002 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mfran2002 mfran2002 is offline Offline
Newbie Poster

[DELPHI] how to set event procedure runtime?

  #1  
May 7th, 2008
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;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: [DELPHI] how to set event procedure runtime?

  #2  
May 7th, 2008
Originally Posted by mfran2002 View Post
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:
  1. 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:
  1. ...
  2. type
  3. TMainForm = class(TForm)
  4. ...
  5. public
  6. procedure OnTimeEvent(Sender :TObject);
  7. end;
  8.  
  9. var
  10. MainForm :TMainForm;
  11.  
  12. implementation
  13. ...
  14. procedure TMainForm.OnTimeEvent(Sender :TObject);
  15. begin
  16. ... // your code here
  17. end;
  18. ...
Now, You must adjust your code above:
  1. var
  2. Timer1: TTimer;
  3. begin
  4. Timer1.Create(nil);
  5. Timer1.Interval := 2000;
  6. Timer1.OnTimer := MainForm.OnTimerEvent;
  7. 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
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: mfran2002 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mfran2002 mfran2002 is offline Offline
Newbie Poster

Re: [DELPHI] how to set event procedure runtime?

  #3  
May 8th, 2008
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
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: mfran2002 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mfran2002 mfran2002 is offline Offline
Newbie Poster

Re: [DELPHI] how to set event procedure runtime?

  #4  
May 8th, 2008
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!!!!
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: [DELPHI] how to set event procedure runtime?

  #5  
May 8th, 2008
Originally Posted by mfran2002 View Post
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:
  1. program AppTimer;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. Dialogs,
  5. Windows, WinTypes;
  6.  
  7. var
  8. Counter :Integer;
  9.  
  10. // callback function for Timer
  11. procedure TimerProc(wnd :HWND; // handle of window for timer messages
  12. uMsg :UINT; // WM_TIMER message
  13. idEvent :UINT; // timer identifier
  14. dwTime :DWORD // current system time
  15. ); stdcall; // use stdcall when declare callback functions
  16. begin
  17. Inc(Counter);
  18. WriteLn('OnTimer passed...', Counter);
  19. end;
  20.  
  21. var
  22. Number :Integer;
  23. TimeHandle :UINT;
  24. Msg :TMSG;
  25. begin
  26. Counter := 0;
  27. TimeHandle := SetTimer(0, 0, 1000, @TimerProc);
  28. if TimeHandle > 0 then
  29. try
  30. while (Counter < 10) do
  31. begin
  32. GetMessage(Msg, 0, 0, 0);
  33. DispatchMessage(Msg);
  34. // uncomment this code portion and see the result
  35. // Write('Input some number: '); Readln(Number);
  36. end;
  37. finally
  38. KillTimer(0, TimeHandle);
  39. end;
  40. end.
I hope this help you.
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: mfran2002 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mfran2002 mfran2002 is offline Offline
Newbie Poster

Re: [DELPHI] how to set event procedure runtime?

  #6  
May 9th, 2008
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!!!!
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: [DELPHI] how to set event procedure runtime?

  #7  
May 13th, 2008
Originally Posted by mfran2002 View Post
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
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: mfran2002 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mfran2002 mfran2002 is offline Offline
Newbie Poster

Re: [DELPHI] how to set event procedure runtime?

  #8  
May 14th, 2008
thanks a lot
again
Reply With Quote  
Join Date: Oct 2006
Posts: 3
Reputation: luis_ramos is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
luis_ramos luis_ramos is offline Offline
Newbie Poster

Re: [DELPHI] how to set event procedure runtime?

  #9  
May 26th, 2008
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;
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 9:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC