Solved

Keep console application alive with timer???

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

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 156
find a node in VST 2 92
how to update exe applicatio from internet ? 6 106
Broadcast a message using ICS 2 50
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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