Hello everyone.

As the title suggests i need to implement a type of timer based function inside a UI Thread in MFC.
Although CWinThread is a UI Thread and has a message pump it does not have a Timer function as CWnd::OnTimer

Is there any way i can implement a timer based function inside a CWinThread, being also able of course to accept messages coming from other threads?

I tried using a while loop (with or without a Sleep() call) inside CWinThread::Run function but it appears that this technique is preventing the messages to be received. (I've overriden PreTranslateMessage and it's never being called)

I also tried to utilize the CWinThread::OnIdle but the results are kind of the same.

In conclusion there is no way to implement a timer based check inside a CWinThread and be able to receive messages from other threads at the same time.
Or is it?

You could ask me why i do not use a worker thread instead (since i don't actually create any graphical content inside my CWinThread), but i would like to send messages to the thread, and avoid using shared/global variables for communication.

So is there anyway to handle this?
I would like any suggestions.

I hope my description is clear enough.
:D

Thanks in advance!

it does not have a Timer function as CWnd::OnTimer

Not to worry, SetTimer() is a global win32 api function, so just call that in CWinThread.

Is there any way i can implement a timer based function inside a CWinThread, being also able of course to accept messages coming from other threads?

Messages are thread specific, one thread can not get another thread's messages. See Remarks here

would like to send messages to the thread

CWinThread is not going to help you with that. Just create a worker thread and call PostThreadMessage()

CreateTimerQueue

Perhaps CreateTimerQueue, CreateTimerQueueTimer, DeleteTimerQueueTimer would do what you want. Plus it's easy to wrap them into a class.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682483%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003%28v=vs.85%29.aspx

Thanks i will try that.

Not to worry, SetTimer() is a global win32 api function, so just call that in CWinThread.

Did try it but no WM_TIMER messages ever arived in the PreTranslateMessage.

Messages are thread specific, one thread can not get another thread's messages. See Remarks here

I don't want to get another thread's message. I want my CWinThread to be able to accept messages through PostThreadMessage.

CWinThread is not going to help you with that. Just create a worker thread and call PostThreadMessage()

I think not. Worker threads have no message queue as far as i understand.

I don't want to get another thread's message.

My misunderstanding. In that case yes, CWinThread will probably work. As for worker threads, you can put a message pump into them so that they can receive messages. Not usually done though.

As for worker threads, you can put a message pump into them so that they can receive messages. Not usually done though.

Nice to know that.

Anyway i found this:
http://forums.codeguru.com/showthread.php?393149.html
and it seems to work. SetTimer actually works on a CWinThread, i just had to override it and declare a message map. I thought since it was not a class member it wouldn't work but it does!

I just hope it doesn't blow up in my hands eventually!
:)

Thanks for the insight!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.