Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Keep console application alive with timer???

Posted on 2004-10-10
5
Medium Priority
?
1,597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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