Solved

Keep console application alive with timer???

Posted on 2004-10-10
5
1,528 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 320
Multiple image collision 13 80
creating threads in delphi 1 136
Unique identifier on a terminal server (rdp) 4 67
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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