Recent

Author Topic: TTimer in Console app?  (Read 8896 times)

AndieSphinx

  • New Member
  • *
  • Posts: 23
TTimer in Console app?
« on: June 04, 2007, 09:50:01 pm »
Hi all,

I need to use a timer in my console application (no forms!).
I've created a TTImer, and an OnTimer event for it:

Code: [Select]
program TimerInConsole;
uses
...
var
 Timer: TTimer;
...

procedure MyTimer;
begin
  // do something
end;

begin
  Application.Initialize;

 Timer:= TTimer.Create(nil);
 Timer.Enabled:= false;
 Timer.OnTimer:= MyTimer;
...
end;


The problem is, that I got an error at the Timer.Ontimer line:

Incompatible type for arg no. 1: Got "untyped", expected "<procedure variable type of procedure(TObject) of object;StdCall>"

If I try it with:
Code: [Select]
procedure MyTimer(Sender:TObject);
begin
  // do something
end;
 

Then I got the error:
Wrong number of parameters specified for call to "MyTimer"

So I need to write something after  Timer.OnTimer:= MyTimer;
But what? If I try  Timer.OnTimer:= MyTimer(nil);  - then I got:
Incompatible type for arg no. 1: Got "untyped", expected "<procedure variable type of procedure(TObject) of object;StdCall>"

Please help, how to create my timer?  :cry: [/quote]

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
RE: TTimer in Console app?
« Reply #1 on: June 04, 2007, 10:33:42 pm »
There are many problems in this code.
You shouldn't do it like this in console apps.
Go in a main loop and do your timing or whatever work from there.

Your concrete questions (But don't do it this way!):
Timer.OnTimer:= @MyTimer;
But then you don't have a procedure(TObject) of object. You'd need a wrapper class for this.
Then in Console Apps you typically don't have Application.Initialize.

It's a mess. ;-)

AndieSphinx

  • New Member
  • *
  • Posts: 23
RE: TTimer in Console app?
« Reply #2 on: June 04, 2007, 10:59:06 pm »
Thanx theo for the quick answer.
Yes, I know, its full with mistakes :), sorry.
I've made this console from a normal application.

The reason:

I'm developing a program for PDA. It has to start another application, wait 60 seconds, and exiting.
My problem is, that if I make a normal consol app, and do some
for i:=0 to 60 do
  sleep(1000);
in the main program, the another application freezes, or even don't starts.
I think, my consol app doesn't pass the processor, or something to the another app.

If I make a fake-console app, from a normal Application (I'll get the Application class, and the Processmessages procedure, but I don't need form!!!), then I can write:
for i:=0 to 60 do
begin
  sleep(1000);
  application.processmessages;
end;
Now, it works fine, and the other app starts fine.
But this is an ugly solution, it would be better, if I could use timer, and in the OnTimer do my finishing job, and exiting (application.terminate).

Can you help me to make a short example to create this timer, and call it normal way?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
RE: TTimer in Console app?
« Reply #3 on: June 04, 2007, 11:57:11 pm »
I don't know what's going on in your case.
How are you executing the external app? TProcess?
Have you read this http://wiki.lazarus.freepascal.org/Executing_External_Programs ?

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
RE: TTimer in Console app?
« Reply #4 on: June 05, 2007, 09:16:21 am »
Since I don't know why you app needs to wait 60 seconds I can't be sure that this approach will be useful, but if you are waiting for the 2nd app to produce a file the first app can use, in Dos days we would delete any occurance of the file that may exist - and then loop attempting to open the file.  With {$i+ (or -)} you can loop until an open of the file is successful and filesize is greater than zero.  Of course, the number of retries should be limited so that the first app doesn't continuously loop if the 2nd app fails.  This approach can be useful if the 2nd app doesn't necessarily terminate once the file is created.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

AndieSphinx

  • New Member
  • *
  • Posts: 23
TTimer in Console app?
« Reply #5 on: June 05, 2007, 01:40:20 pm »
Hi guys, thanx for the feedbacks.

I'm using   CreateProcess(@appath, Nil, Nil, Nil, False, CREATE_NEW_CONSOLE, Nil, Nil, StartupInfo,ProcessInformation);  -to start the second app.

I'll try to clarify the situation, so you can understand me completely. - I hope.

There is a navigation program, sold more than 100.000 here, in Hungary. A very useful, nice and clever navi app.
One of my friend has made a skin pack for it. It uses completely different, more beautiful, colorful bmps, sounds, etc.
The navi program stores these data in a zip file on the pda's storage card, called: data.zip.
To install the new skin pack, you have to replace this data.zip with the new one - its simple.
But. We don't want to give this pack for free, so we need some copy protection.
My idea was, that we will "corrupt" the data.zip (skinned version), so it isn't usable this way. One can't uncompress it, and of course, the navi program even can't start with it. Its completely useless in this corrupted state. (I've overwrote some bytes in the data.zip)
So we'll need a starter application, which will
1. "uncorrupt" the data.zip
2. start the navigation program
3. waits some seconds, till the navi program opens, reads, and closes the data.zip
4. corrupts the data.zip again
5. closes

The navi program stays alive, and works fine, noone will see, that my program works in background, and manipulates the data.zip.

I've tried TProcess, but the navi program won't start with it. I don't know why. Createprocess, wrote above, works fine, the navi prog starts, and my program waits some sec, and do his job.
But if I don't write Application.ProcessMessages while waiting, my program blocks the navi prog, it won't start - or very slow, and usualy stucks somewhere under initializing.
So I tried the trick above, to wait 1 second - processmessages - wait 1 sec - processmessages - and so on.
Now it works fine, but I've found this solution a bit... ugly. Not so perfect.

I've made a small sample app with form, and a TTimer on it. I've started the navi app, and set to timer to 10 seconds, and start it.
The navi app started fine, and after 10 seconds my TTimer has triggered, and corrupted the data.zip, and exited.
I need this solution, but I don't need a form. My app should work in background, invisible.

So. How can I add a TTimer to a non-form-based application?
Or. What else solution do you advise?

Thanx guys for the patience.

 

TinyPortal © 2005-2018