Creating a Watch Dog Application

Environment: C++, Frameworks

The accompanying code takes care of another application (call it "myApp") that should always be running.

It wakes up every X times, and checks the following:

  1. Is myApp running? If not, it turns it on.
  2. Is myApp hanging? (This is decided by myApp not consuming any CPU usage during the last X times and since the previous check.) If yes, kills it and restarts it.
  3. Is myApp already running for Y time? If yes, kills it and restarts it.

The program keeps a log file of its actions (checking, restarting, killing). It also copies the log file of myApp (if any) to another location (same name, plus date and time concatenated), so that the information in it will not get lost.

This code was compiled under VC++ 6.0. It needs the following definitions:

  • _WIN32
  • WIN32
  • _DEBUG
  • _MBCS


Download source - 3 Kb


  • some people feel they r super human

    Posted by dharani on 06/05/2004 04:18am

    so we need to teach them a lesson when they come out with such what-is-this-code ? sort of remarks . i hope these kinda ppl ill opt out of codeguru for its own sake , bye the bye -i ha been coding for 3 yrs ( thoiugh i know how to kill /create process ) this piece of code is very good to me . good luck author keep it up regards dharani dharani

  • VB6

    Posted by Legacy on 02/09/2004 12:00am

    Originally posted by: Anne

    Hi, does anybody have an WATCHDOG for Visual Basic? Thanks, Anne

  • of course it can be killed...

    Posted by Legacy on 12/06/2002 12:00am

    Originally posted by: Johan Eliasson

    Where did he write that this code protects your app from being killed by other apps? NOWHERE! I don't think that's the purpose of the program.

    I have this situation: On a remote computer I need an app running 24/7. The app crashes sometimes by simply quitting without saying anything. If that happens I need to make sure that it's started again.

    This program seems to be exactly what I need.


  • good!

    Posted by Legacy on 12/03/2002 12:00am

    Originally posted by: wolfbin

    About the comments... I think the code is selfcommented, so if you are a coder you don't really need much more. ;)

  • Stones in Glass houses

    Posted by Legacy on 12/03/2002 12:00am

    Originally posted by: JoAnn

    What is it that makes some people think that it's okay and acceptable to bash someone just because they don't find their contribution useful or "ingenius". I don't come to to find some obscure way to code. I come here to learn a new technique and to find solutions. If I already know something, I think, wow, I already know that. If I think I have a better solution, I might contribute it for others to benefit from. I would NEVER dream of bashing someone cause I disagreed with their method, their code style, etc. In fact, I find that I am taken more seriously if I share an honest critique without going out of my way to be rude. I don't think that Tali or any other person submitting code for others' use or edification should become the target of judgemental comments. If you don't like the idea of using API calls, where is your submission addressing the same issue without the API calls? And as for College-ish, well, I have been coding for 4 years now and on occasion I still pull out an old project to refresh an idea or recall how I did something before. I think the purpose here is to provide differing insights, not to tear apart the coder.


  • Very useful

    Posted by Legacy on 12/02/2002 12:00am

    Originally posted by: Marc Clifton

    I had to do something like this recently, and I found it interesting to see your approach.

    Others have posted what I think are incredibly rude comments. You've put together a simple and clean app that does one thing, and one thing well. That's rare. And anyone worth their salt shouldn't need code comments to understand immediately what you're doing. Good grief. Ignore those morons.


  • Screw the gum-flappers...

    Posted by Legacy on 11/30/2002 12:00am

    Originally posted by: Cloaca

    Don't be discouraged by the gum-flappers here who are not significant. Nice helpful work Tali. Simple, but exactly what some folks (like me) will need.


  • It is junk

    Posted by Legacy on 11/29/2002 12:00am

    Originally posted by: Abdul Kareem

    What is this??? Some junk of API calls.. No comments, No explanation. We are not expecting this type of article at

  • It seems like some explanations are needed here...

    Posted by Legacy on 11/28/2002 12:00am

    Originally posted by: Tali Gat-Falik

    The idea of this article was not to present any "ingenous" code (these parts are considered my company's IP...), but to implement something I needed a few times in the past.

    It's true that I mainly use API calls , but I hope that the framework and putting it all together could help others, even if "help" means saving one hour of coding+debugging...

    By the way, the time calculation there is not trivial - do you know it off-handedly taht each high byte is ~7 minutes?
    otehrwise the transaltion between system time and real minutes is a headacke...

  • support comment

    Posted by Legacy on 11/28/2002 12:00am

    Originally posted by: sway

    I have been using MFC mostly. This API code is simple but helpful to me too.
    I believe that the is not only a place like "GURU" club, but also a place where programmers of all levels can find help.
    What is the standard in here? Being able to help some other people is the standard in here. Why this site is the #1? because it's open minded to all ideas, even worng ones.


  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • In this study, conducted by Forrester Consulting and commissioned by BMC, you'll find out why 93% of US and UK IT decision-makers surveyed consider capacity management important or very important to their cloud strategy. Learn how using capacity management to manage workloads in the cloud can help Increase flexibility and continuity for your organization.

  • On-demand Event Event Date: June 23, 2015 Business transformation is not just important, it is vital to the success of all organizations in the 21st century marketplace. How IT approaches and executes the move from cost center to revenue contributor will not only transform the IT organization, it will change how companies retain competitive advantage and meet the requirements of internal and external customers. This shift in thinking is not new, but actualizing it is. Check out this eSeminar to learn the …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date