Thread: Automatically Running Program

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Automatically Running Program

    H,

    I was wondering if anyone knew how to make your program run automatically, for example every Wednesday or something? I know I could schedule a task inside Windows, and I could do that if there was no other way, but I was hoping that I could do it inside the program. Doe s anyone know of a way? Thanks.

    BTW, I also wanted to say that I'm not going to use this for a virus type thing or anything like that. I'd like it to create a backup of some files every week, and I'd like to do that without an external program. I just thought I'd clarify that because I know that asking questions like this can sometimes lead to trouble...

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    In short, somethign has to be running in order to tell your program to run. The most common way to do this is to have your program run at startup by puttign it into the startup folder, and write a timer function that checks the system time every minute or so. If its not whatever time you want it to run, it does nothing, if it is the right time, then it does whatever you want. This si allpretty basic windows programming.

    in your WM_CREATE case -
    Code:
    TimerId = SetTimer(hwnd , 1 , 60000 , NULL);
    then create a WM_TIMER case -
    Code:
    if(ItsTime){
         // your code goes here
         }
    There are 'fancier' ways to do this, but Im guessing you are just getting into windows programming, so Ill spare you teh information overload.
    Last edited by abachler; 10-30-2007 at 03:48 PM.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well really operating systems are built to do that, ya know. It's the program that manages the system, therefore does it not make sense for it to be handling scheduling?

    If your logic must remain so elegantly twisted then consider a small watcher program (a service) that sits there and periodically queries the date. If the date matches what you're looking for, launch the main program.

    If you're gonna do this, can I pleasepleasepleasepleasepleasePLEASE suggest that you name the watcher program something sensible, that way it doesn't end up getting mentioned on every spyware forum in the world...

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I would not recommend a beginner try to do this with a system service. A mistake in a system service can crash the OS so bad you have to reformat your hard drive and reinstall windows. A bad pointer in a user level program just causes windows to shut down the offending program, a bad pointer in a kernel mode program does whatever it wants.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You mean D347H53RV1C3.exe isn't a good name for my watcher service?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by abachler View Post
    I would not recommend a beginner try to do this with a system service. A mistake in a system service can crash the OS so bad you have to reformat your hard drive and reinstall windows. A bad pointer in a user level program just causes windows to shut down the offending program, a bad pointer in a kernel mode program does whatever it wants.
    A service doesn't have to run in kernel mode.

  7. #7
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    or you can simply use the "at" command to run your program at a specific date/time. just open up the console and enter "at /?" to get it's usage instructions. note the "/every:date" argument.

    if you wanted to do this from inside your program simply use CreateProcess or ShellExecute to execute the command.
    Last edited by Bleech; 10-30-2007 at 09:50 PM.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    My hat is off to you, never knew about that command before. We need a points system on this board like they have over at some other boards, like the one where my title is Legendary Pharmer

    Couple questions though, how do you see what tasks are scheduled and how do you remove them?

    Guess I need to do a refresher on my command line commands.
    Last edited by abachler; 10-31-2007 at 08:06 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Control panel "scheduled tasks" will show that. You can also right-click there to add new tasks with the GUI instead of using the "at" command [if you prefer GUI].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My hat is off to you, never knew about that command before.
    It's yet another Linux command that Windows decided to adopt.

    One thing I never liked about the Windows task scheduling, however, is that the computer has to be on for the task to execute. (At least it did the last time I tried it, with Windows 98, and it probably hasn't changed since then.) So if you decide not to turn on your computer on Wednesday, the backup won't happen. That might be a problem.

    Here's one way you could fix it. Schedule your program to run on Wednesday at a given time. Write your code so that it records the last time you did a backup. Then write a program that executes on startup which checks this time -- if it was over a week ago, do a backup immediately.

    Or, depending on how you want your backups set up, you could then schedule the next backup to be exactly a week from now -- making your backups at least 7 days apart. The disadvantage of this, of course, is that a backup might happen all of a sudden when you're in the middle of something, instead of predictably on Wednesday.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    It's yet another Linux command that Windows decided to adopt.

    One thing I never liked about the Windows task scheduling, however, is that the computer has to be on for the task to execute. (At least it did the last time I tried it, with Windows 98, and it probably hasn't changed since then.) So if you decide not to turn on your computer on Wednesday, the backup won't happen. That might be a problem.
    Technically, the "at" command existed in Unix a long time before Linus wrote the first line of Linux, but yes, it's existed in Linux/Unix for a long time.

    The point about the machine having to be on will have to be, right? If you pull the power out of the wall [on a non-laptop], how would the machine be able to do something? Ok, so you may say that you could have it switch on, if the power is plugged in, but as far as I know, there's no standardized functionality to do that - there is "Wake-On-Lan" and such remote wake-up mechanisms, but as far as I know, there's no "set the system to power on at Wednesday 7th Nov 23:00" - and it would have to be "AutoLogon as user X" too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by matsp View Post
    Control panel "scheduled tasks" will show that.
    easy to do programatically...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  2. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  3. Running program in unix
    By Cpro in forum Linux Programming
    Replies: 2
    Last Post: 02-10-2008, 09:28 PM
  4. How to tell when internal program is done running
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 03-21-2005, 10:09 PM
  5. FAQ : running program inside program (spawn)
    By nipun in forum C Programming
    Replies: 3
    Last Post: 06-13-2004, 02:30 PM