The information in this article applies to:
SUMMARY
The TMRPROC sample is designed to demonstrate how to use the SetTimer() API
in an MFC Application in such a way that the TimerProc() callback function
(a member of a C++ class) is used instead of handling WM_TIMER messages.
There are two makefiles included:
MORE INFORMATIONThe following file is available for download from the Microsoft Software Library: ~ TMRPROC.EXEFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base: Q119591 How to Obtain Microsoft Support Files from Online ServicesNOTE: When extracting files from TMRPROC.EXE, be sure to use the -d option to create subdirectories. The challenge of using callback functions as C++ class members can be approached in several different ways, depending on the circumstances surrounding the callback. In "Calling All Members:..." (see References below), four different approaches are mentioned. They are:
TMRPROC.CPP and .HStoring the "this" pointer to the object in a static member is demonstrated in the CWinApp-derived class, CTmrprocApp.There is no window associated with this class, so the output used to indicate the timer event is done via OutputDebugString(). The pThis variable is a static member variable that is initialized in the constructor to contain the "this" pointer for the class. The TimerProc() callback is simply a static member function of type "void CALLBACK EXPORT." It is static, so no "this" pointer is passed in. For this reason, the pThis member variable is used to store the CTmrprocApp's "this" pointer. Whenever TimerProc() gets called, the current instance of CTmrprocApp can be referenced through the CTmrprocApp::pThis member. This is the simplest case. There are three functions added to the CTmrprocApp class to accomplish this objective - TimerProc(), CTmrprocApp::OnAppsettimer(), and CTmrprocApp::OnAppkilltimer(). The later two are the menu handlers that allow the user to start and stop the timer. TMRPRVW.CPP and .HStoring a list of "this" pointers mapped to the list of objects servicing the callback in a static structure is implemented in the view class, CTmrprocView.In this case, a little more work is required, but not much. Because a timer needs to be set for each instance of the view class, a scheme must be implemented to associate each timer ID with a corresponding view object. The view object is identified by its "this" pointer and the timer by its timer ID (passed back from SetTimer()). The view class associates these and stores them in a CMapWordToPtr static member variable. This way, when each view's TimerProc() is called, it can first look up its "this" pointer in the pointer/ID map, and then update its own members. There are several functions needed to start, stop, and reset the count on the timer. The count is just a running count of the number of times TimerProc() has been called for the current view. The following are the start, stop, and reset functions:
The following are some "all" type functions that can be used to start,
stop, and reset the timer for each view:
REFERENCESMSDN Technical Articles, C/C++ Articles, "Calling All Members: Member Functions As Callbacks," by Dale Rogerson. Additional query words: kbfile tmrproc settimer
Keywords : kbMFC |
Last Reviewed: July 26, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |