Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Why is it so hard to find a working example on how to use the timer in your C++ app?
So let's take a look at some code:
Changes to stdafx.h
First you need to change the stdafx.h file, you can simply copy and paste the following into a console c++ app:
#pragma once
/* Tells the system what version the code can run on
* actually just refers to another header file: sdkddkver.h */
#include "targetver.h"
/* <stdio.h>
* is a file which defines the structures, values, macros, and functions
* used by the level 2 I/O ("standard I/O") routines. It is an ansi standard library */
#include <stdio.h>
/* <tchar.h>
* Definitions for generic international functions, mostly defines
* which map string/formatted-io/ctype functions to char, wchar_t, or
* MBCS versions. To be used for compatibility between single-byte,
* multi-byte and Unicode text models. */
#include <tchar.h>
/* <iostream>
* Header for I/O
*/
#include <iostream>
/* <Windows.h>
* Master include file for Windows applications.
* Interesting reading for sure*/
#include <Windows.h>
/* using namespace std;
* The keyword using is used to introduce a name from a namespace into the current declarative region.
* using namespace std; means you don't have to write std::cin
* on the other hand, a lot of developers do not feel that this is a good practice
* your choice, I use it. */
using namespace std;
Now for the source file
For now let's keep with the console app, I don't like it, but it will keep the code a little cleaner, this is a mild modification of the example code at: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687008(v=vs.85).aspx , which doesn't include the stdafx.h file corrected like the above one.
I will be using this or similar to show how to use a timer in a game.
#include "stdafx.h"
int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER largeIntLengthOfTime;
largeIntLengthOfTime.QuadPart = -50000000LL;
// Create an unnamed waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
if(NULL == hTimer)
{
/* If the CreateWaitableTimer didn't work, then the hTimer will be null
* Note that GetLastError is found in the errhandlingapi.h (thank you Captain Obvious) */
printf("CreateWaitableTimer didn't work (%d)\n", GetLastError());
return 1;
}
printf("Period = 5s...\n");
/* Wait for a 5s period. This uses the synchapi.h header file
* This is a function see: https://msdn.microsoft.com/en-us/library/windows/desktop/dd405521(v=vs.85).aspx
*
*/
if (!SetWaitableTimer(hTimer, &largeIntLengthOfTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}
/* Wait for the timer to signal, the INFINITE keyword indicates that the timer must signal for the
*/
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("The timer has signaled the one time it will.\n");
return 0;
}
And a little gift from Chicago:
Comments
Anonymous
May 28, 2013
What about using the DispatcherTimer Class? msdn.microsoft.com/.../windows.ui.xaml.dispatchertimerAnonymous
May 29, 2013
Darn you Jim Boddie, but there is an issue with that one, mainly I didn't cover it. I just hate it when a reader comes up with a great idea. Good work, keep reading.