Solved

Keep console application alive with timer???

Posted on 2004-10-10
5
1,511 Views
Last Modified: 2012-06-21
Ok how do I make a console application with 2 timers on. I also want it not to die.
if i hadd ReadLn OR while true do sleep(100);
it will keep my app alive, but freeze my timers.
[PLEASE POST FULL CODE]



My code untill now:



program testing;

{$APPTYPE CONSOLE}

uses
  windows,sysutils;


  var
  MyTimerHandle:WORD;



  procedure TimerProc();stdcall;
begin
writeln('it doesnt work !!');

end;


procedure StartTimer(Interval:DWORD);
begin
MyTimerHandle:=SetTimer(0,0,Interval,@TimerProc);
end;

procedure StopTimer;
begin
if MyTimerHandle<>0 then begin
   KillTimer(0,MyTimerHandle);
   MyTimerHandle:=0;
end;
end;


begin
starttimer(1000);

end
0
Comment
Question by:ssivagna
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
ID: 12271812
Yeah, welll... Since you don't keep your application running, it will just end as soon as it reaches the last line. The thing I often do is just create a messageloop in my application, with a Window connected to it. An interesting example of this can be found at http://www.workshop-alex.org/wallpaper/wallpaper.html where you find the sourcecode of a small tool I created that changes the wallpaper. Here I also use the messaging system instead of a timer procedure, which is more useful since I already have a messageloop running.
The drawback of course is that my application keeps running in this loop:

  while (GetMessage(aMsg, AHandle, 0, 0)) and not StopApp do begin
    TranslateMessage(aMsg);
    DispatchMessage(aMsg);
  end;

Thus, if I want my code to do something else too then I will need to create a separate thread for that action.

Another problem is your callback routine, of course. According to MSDN it should have this (C++) syntax:

  VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

And your procedure just doesn't have these parameters.

But personally I keep my application alive by including a messageloop to them and then wait until it tells me to end the application. Check out the wallpaper example.
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12271853
Why doesn't SetTimer work in a console app?
===============================
All SetTimer does is tell the system to stick WM_TIMER messages into your window's/thread's message queue after all other messages have been processedand the timer has expired: DefWindowProc secretly invokes the function when it gets this message. Lacking such a queue or a GetMessage()/DispatchMessage() pumping loop the messages never get delivered.
Use Sleep(), WaitForSingleObject() and Waitable timers instead

0
 

Author Comment

by:ssivagna
ID: 12272431
what I want is a fixed version of my code. not link to some application.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12272879
Yet you accepted my answer? Well, the link does just link to an application that has a timer, similar to what you wanted.
0
 

Author Comment

by:ssivagna
ID: 12276699
i accepted it because I found an answer to it in other way. Yet your code wasnt exactly what I wanted
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now