Solved

Keep console application alive with timer???

Posted on 2004-10-10
5
1,480 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
Comment Utility
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
Comment Utility
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
Comment Utility
what I want is a fixed version of my code. not link to some application.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
Comment Utility
i accepted it because I found an answer to it in other way. Yet your code wasnt exactly what I wanted
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

17 Experts available now in Live!

Get 1:1 Help Now