Open Side Menu Go to the Top
Register
C++ Timers Question C++ Timers Question

03-29-2011 , 08:07 PM
Not sure if this is allowed or not but I have been struggling with some code for a couple days and wanted some guidance.

I am just getting started as a programmer and doing some contract work in c++ using visual studio.

My current task was to create a basic application that would check to see if a program was running every 15 minutes and if not to launch it and write to a log file that we had to launch said program.

I have successfully made the log and created an application that when I click a button it does everything I need it to do.

The problem comes in when I try to execute this code every 15 minutes.

How does one go about doing this? Like any concrete examples?


Also is there a way to get better at this in general? I seem to be struggling with string conversion and using ATL vs MFC ?

This job has been work from home mostly with little guidance so I feel like i really need to improve some way.
C++ Timers Question Quote
03-29-2011 , 08:13 PM
cron
C++ Timers Question Quote
03-29-2011 , 08:15 PM
Quote:
Originally Posted by Neil S

im developing for a windows xp / 7 client, mainly using the windows api.

From what i have read I need to use
UINT_PTR WINAPI SetTimer(
__in_opt HWND hWnd,
__in UINT_PTR nIDEvent,
__in UINT uElapse,
__in_opt TIMERPROC lpTimerFunc
);

but havent been able to find an example that shows how to create the handler etc.
C++ Timers Question Quote
03-29-2011 , 08:21 PM
TIMERPROC is likely a function pointer. Look up its specific definition and create a handler function with matching arguments and return type.
C++ Timers Question Quote
03-29-2011 , 09:02 PM
Quote:
Originally Posted by Jeff_B
im developing for a windows xp / 7 client, mainly using the windows api.

From what i have read I need to use
UINT_PTR WINAPI SetTimer(
__in_opt HWND hWnd,
__in UINT_PTR nIDEvent,
__in UINT uElapse,
__in_opt TIMERPROC lpTimerFunc
);

but havent been able to find an example that shows how to create the handler etc.
If you haven't already found it, the msdn docs are extremely useful for such things. http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx is the first google result for only "SetTimer" - but I'd usually do "SetTimer msdn" or add "Winapi" etc. It links to the following page, which hopefully has enough examples to cover your scenario: http://msdn.microsoft.com/en-us/libr...creating_timer

TIMERPROC is indeed a function pointer, but if null it will post a WM_TIMER message to the main message loop. the last example on the linked page shows using settimer to call a function.
C++ Timers Question Quote
03-29-2011 , 11:36 PM
I'm taking a high performance programming class in C++ right now. Naturally, to make any of our assignments useful we need to time various techniques. There's a few timers we use that vary in difficulty in setting up as well as accuracy. The one that seems to be the most accurate without being a hassle to use is gettimeofday(). I could even post sample code but I'm supposed to be studying for a test

Maybe if there's time later I'll throw up something quick. Otherwise, you can google. Just know that if someone adjusts the system clock (or does a similar action) the results from gettimeofday() will be inaccurate because it uses the system clock to literally get the time of day (you subtract end time from start time to get the elapsed time).
C++ Timers Question Quote
03-30-2011 , 02:11 AM
Unless you need to do something else in your code during those 15 minutes then it would be much easier to just use the Sleep() function:

http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

Code:
#include <Windows.h>

.
.
.

for (;;) {
  <execute code you want to perform periodically>
  Sleep(15*60*1000);
}
Juk
C++ Timers Question Quote
03-30-2011 , 04:15 AM
Also as a quick solution, I think you can set up windows to run it every 15 minutes, I'm not very good with Windows so don't know exactly how to do this but it should be easy.
C++ Timers Question Quote
03-30-2011 , 04:28 AM
Quote:
Originally Posted by Gullanian
Also as a quick solution, I think you can set up windows to run it every 15 minutes, I'm not very good with Windows so don't know exactly how to do this but it should be easy.
Yeah you can: go to "Task Scheduler" and create a new task. You'll have the option to "repeat every" and should be able to set it up that way.

Juk
C++ Timers Question Quote
03-30-2011 , 06:59 PM
Quote:
Originally Posted by jukofyork
Yeah you can: go to "Task Scheduler" and create a new task. You'll have the option to "repeat every" and should be able to set it up that way.

Juk

And since this was was supposed to be a quick testing thing that would have been probably brilliant a week or so ago and i could have had like an hour turn around time (I had other priorities the last week)

my problem thus far has been creating the handler.
Not sure why, though I have never learned windows programming sort of teaching myself using msdn and experimentation.
C++ Timers Question Quote
03-30-2011 , 07:28 PM
Quote:
Originally Posted by Jeff_B
my problem thus far has been creating the handler.
Not sure why, though I have never learned windows programming sort of teaching myself using msdn and experimentation.
can you elaborate on this? I'm not getting what you mean by "handler". tho it's been years since I've done this sot of stuff. Presumably you are doing this so you know how to do it now?

FWIW, I'd have used AHK for this, it's done in a few lines of script. If you find yourself having to do similar tasks often for work, AHK is a handy tool for very quick solutions to problems like this. But of course not very useful if your goal is to learn making Windows applications in C
C++ Timers Question Quote

      
m