Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Timer in CWinApp

83 views
Skip to first unread message

Kai Schülke

unread,
Mar 12, 2001, 8:15:55 PM3/12/01
to
Hello,

I am trying to write a MFC/MDI App which reads a socket in CMyApp and
displays the data in CMyView. Because I want to keep the system-load down
CMyApp::ReadSocket shall be called only every ...say... 40ms. I see the
following solutions to my problem:

- Call a Timer in CMainFrame (not in ChildFrame because I have several
ChildFrames which sould be updated sychron)
- Write a main-function in CMyApp that does an endless loop as seen below.
pseudocode:
CMyApp::Main
{
ReadSockets;
DrawChildFrames;
Sleep(40);
}

I think that the best aka cleanest solution would be an timer in CMyApp
(with CMyApp::OnTimer()), but I don't know how to do this. Is it even
possible? (If I read the hierarchy chart correctly it isn't)
If that isn't possible which of the two ways above is better. I would go for
the timer in CMainFrame, but I am a newby so I would be glad if someone
could view eventual fallstricks/traps/...

Thank you

Kai Schuelke


Scott McPhillips

unread,
Mar 12, 2001, 10:29:53 PM3/12/01
to

No, it's not possible to SetTimer and handle WM_TIMER in the app class. The
timer inherently needs a window to post the message to, and MFC does not route
that message to apps and docs. It's also a bad idea to put an endless loop in
the main thread - it will stop processing of user input.

Handling the timer in the CMainFrame, and calling an app function from OnTimer
if you like, is just about all you can do and should work fine.

Incidentally, your endless loop approach would be OK if you put it in a
secondary thread.

--
Scott McPhillips [VC++ MVP]

Sam Hobbs

unread,
Mar 13, 2001, 8:54:32 PM3/13/01
to
You could probably use a static member of your application class to as a
callback to process the timer events but you will need to have a way to get
to the application. For the application, you should be able to use
AfxGetApp() to get to the application.


"Kai Schülke" <kai.sc...@unibw-muenchen.de> wrote in message
news:3aad6...@news.unibw-muenchen.de...

Joseph M. Newcomer

unread,
Mar 14, 2001, 8:42:37 PM3/14/01
to
This keeps system load down, but also keeps your throughput limited.
Why not use asynchronous sockets, which run full-speed when there is
something there and cost virtually nothing when there is no data?

You can't put a timer in an app unless you use a callback function
(that is, a WM_TIMER message will never be seen by your CWinApp
class). Note that of all the trash provided for a timer callback, the
crucial value, an LPVOID set at the time SetTimer is called, is not
provided (this is because timer callbacks were designed before
object-oriented programming caught on, and everything was kept in
global variables). Check out my essay on callback functions on my MVP
Tips site.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

0 new messages