Please discuss general Delphi programming topics here.

Hi,
I want to count the running time of a procedure (e.g. the procedure button1.click) with the timer on timer1.timer procedure.
But there is a problem : I can't do those 2 procedures at the same time. Any help? Some could would be thankful.
Regards,
hacchan
Active Member

Posts: 6
Joined: December 8th, 2004, 3:30 am
Location: indonesia

Can't you simply add to the procedure Timer as local variable, enable it in first line, and disable and get what you need then?
I think code would look like this:
Code: Select all
procedure xxx;beginvar Timer1: TTimer;beginTimer1:=TTimer.create(?);Timer1.Interval:=1;Timer1.Tag:=0;Timer1.Enabled:=true;...Timer1.enabled:=false;end;procedure Timer1.OnTimer(?);begininc(tag);end;

Question mark means I don't remember what is there (if anything).
I think tag variable isn't used by component itself and you can put there whatever you want.
Simple ideas are the best, but I don't know whether it works or not. Did you try something like that? Or could you just put here your source code?
PS. I'm not responsible for any effect this code will have on your computer (including blowing up).
Active Member

Posts: 24
Joined: May 1st, 2005, 9:32 pm
Location: Poland

Why using a timer?
Kambiz

Kambiz

Posts: 2428
Joined: March 7th, 2003, 7:10 pm

Why not? If you delete Timer from that code, it won't do what it is supposed to. That was first idea I had and if you have better one: go ahead. But I think it should work even if it isn't optimal.
Active Member

Posts: 24
Joined: May 1st, 2005, 9:32 pm
Location: Poland

It doesn't work. The thread is busy and cannot dispatch timer message, therefore no event is generated.

Code: Select all
procedure Foo;var  TimeBegin, TimeEnd: DWORD   ElapsedTime: DWORD;begin   TimeBegin := GetTickCount;  // Do your task here  TimeEnd := GetTickCount;  ElapsedTime := (TimeEnd - TimeBegin); // in millisecondsend;
Kambiz

Kambiz

Posts: 2428
Joined: March 7th, 2003, 7:10 pm

Sounds good. I never know whether GetTickCount works and is named like this. I think that my idea would work in one thread application or if you used some other timer (TThdTimer?). But that is better.