Re: Thread and Timer

Tech-Archive recommends: Fix windows errors by optimizing your registry



I'm not talking about callbacks. I'm talking about using
ON_THREAD_MESSAGE(WM_TIMER,OnTimer)

I guess I will let the code explain it.

class CMyThread : public CWinThread
{
DECLARE_DYNCREATE(CMyThread)

protected:
CMyThread(); // protected constructor used by dynamic creation
virtual ~CMyThread();

public:
virtual BOOL InitInstance();
virtual int ExitInstance();

protected:
afx_msg void OnTimer(WPARAM,LPARAM);
DECLARE_MESSAGE_MAP()

private:
UINT m_TimerID;
};



IMPLEMENT_DYNCREATE(CMyThread, CWinThread)

CMyThread::CMyThread()
{
}

CMyThread::~CMyThread()
{
}

BOOL CMyThread::InitInstance()
{
m_TimerID = SetTimer(NULL,0,5000,NULL);
return TRUE;
}

int CMyThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMyThread, CWinThread)
ON_THREAD_MESSAGE(WM_TIMER,OnTimer)
END_MESSAGE_MAP()

// CMyThread message handlers

void CMyThread::OnTimer(WPARAM nTimerID,LPARAM)
{
if (nTimerID == m_TimerID)
{
MessageBeep(1);
}
}


AliR.



"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:irsit3lmg7rsp21388mvaf17cj43m13jbo@xxxxxxxxxx
To get WM_TIMER messages, yes. This trick is actually pretty common; for
example,
CAsyncSocket creates a dummy window to handle callback notifications,
which are sent as
window messages; SAPI creates a dummy window to handle Text-to-Speech
notifications; and I
knew about the trick years ago. Any generic derived-direct-from-CWnd
class will do
nicely.

Timer callbacks will work without a window, but overall they suck badly,
because there is
no user-specified data object passed in (as I explain to my students, the
callback
function has four parameters, three of which are totally useless and one
of which is never
used anyway, so why do we have four useless parameters? Bad design. You
can't really
make a thread-safe callback without a lot of effort; __declspec(thread)
helps, or direct
use of Thread Local Storage.

My "timer-in-a-CDocument" essay shows how this can be done in a way that
lets a CDocument
maintain an internal timer which has nothing to do with a view, which is
useful if the
"document" is virtual and is implemented at the other end of a
communication wire (RS232,
RS485, USB, etc.) and the timeouts deal with the document's communication
with its virtual
implementation and should not be visible to the views.
joe

On Thu, 13 Mar 2008 14:18:09 GMT, "AliR \(VC++ MVP\)" <AliR@xxxxxxxxxxxxx>
wrote:

Joe you must be kidding, he needs a window in his UI thread just use
timers?

::SetTimer works just fine in CWinThread. All you have to do is set a
ON_THREAD_MESSAGE(WM_TIMER,OnTimer) in your message map.

AliR.


"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:jvcit3hs7gfnce0ttl9vsliqmfbcc2pv4c@xxxxxxxxxx
Is it a UI thread? Is the timer associated with a window in the UI
thread? (You may have
to create an invisible top-level window to get this to work...)
joe

On Thu, 13 Mar 2008 02:42:20 -0700 (PDT), Stefano <posting@xxxxxxxxxx>
wrote:

On 12 Mar, 16:39, Joseph M. Newcomer <newco...@xxxxxxxxxxxx> wrote:

Why not do something very simple: ON_WM_TIMER()?


I tryed with ON_WM_TIMER but it's never called.



You do not need a g_iTmer, g_hWnd, or g_iMessage here, if this were a
handler for
WM_TIMER. Get rid of them. I can't even imagine why they are needed at
all, since they
coudl be member variables of the CWatchDog class.

Yes you are right. I copyed and paste the code from the the callback.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


.



Relevant Pages

  • Re: Thread and Timer
    ... class CMyThread: public CWinThread ... CAsyncSocket creates a dummy window to handle callback notifications, ...
    (microsoft.public.vc.mfc)
  • Re: Custom model in gtkada?
    ... GUI takes over the single thread of control, ... the _spec_ for a callback is located in the same ... The window object is not the state of the program. ... I think you mean the event queue was more visible in JEWL. ...
    (comp.lang.ada)
  • Re: Newbie question on MESSAGE_MAP
    ... What mechanism prevents two DLLs from sending the same message, ... You can "register" a callback any way you want to. ... the window belongs to the process. ... Or is there a way that I can register for callbacks from dlls directly? ...
    (microsoft.public.vc.mfc)
  • Re: Custom model in gtkada?
    ... The concurrency comes from programs needing to do something else and respond to user events at the same time, which is a fairly common situation. ... With a sequential language and sequential thinking, you get the typical C-style callback design, in which the GUI takes over the single thread of control, and you get other things to happen when you're not responding to user events through "idle functions". ... I'm saying each window may have its own thread of control. ...
    (comp.lang.ada)
  • Re: page level onclick - can I detect nearest word
    ... Something like onload, for example, would be registerd on BODY and the browser would map the callback to the window. ... You've argued that registering an event without using an handler attribute requires the HTML element to have an ID attribute, ... While the coupling of element id to script might be less obvious, the argument is based on a false premise that an ID is necessary to register an event. ...
    (comp.lang.javascript)