Re: PeekMessage not working as desired!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See below...
On Thu, 14 Dec 2006 13:14:50 +0100, Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx> wrote:

vkinra@xxxxxxxxxxx wrote:
We have a situation where we need to process UI input in a separate UI
thread rather than the main thread.

There is no such thing as a main thread, all threads are at first equal. If
you use the initial thread or another thread for UI is up to you, just make
sure that you don't share UI stuff between threads.

****
Actually, that's not true in CE. There really is a "main thread", and unlike regular
Windows, if the main thread terminates, the app terminates (it strikes me that the CE
people hadn't read the specifications when they implemented this misfeature).

Which does not change the fact that there is absolutely no parent-child relationship as
the OP seems to want to rely on. Secondary threads are just secondary threads.
****

Ours is a MFC application running on Windows CE. Since,
AttachThreadInput is not available, I tried using PeekMessage.
In MainFrame OnCreate I created a UI Thread using AfxBeginThread.

Okay, at that point, the thread invoking OnCreate is already bound to be the
UI thread.

This thread (AnimationThread) basically displays a busy
icon which the user can hit to cancel an operation in the main thread
(I realize this method is counter intuitive, but we had to do it this
way).

Hmmm, the normal way is to start a worker thread which does the long
operation and then MsgWaitForSingleObject() for that thread to finish,
keeping the message queue of the window alive. You then react to a cancel
request by somehow interrupting the worker thread. If the worker thread is
displaying an animation, you need some shared picture queue where it places
pictures and then tells the UI to update itself with a message.
****
Actually, the normal way is to start a worker thread which does the long computation and
immediately return to the message loop *without* doing a WFSO. Doing a WaitFor...
waiting for a thread to finish defeats the value of having a thread, and blocks the GUI,
and is fundamentally bad design. It is inconsistent with keeping the message queue window
alive. But I agree with you that the real problem stems for doing the design backwards.
****

Note: in such cases, a timer instead of another thread is much easier to
handle!
****
Timers are generally too coarse, and too unpredictable, to support animation effects well.
****

My frustration is that I had this working before but then I
started [...] but in the process, I broke something. Now I cannot
figure out at all why PeekMessage does not work.

Version control systems exist for such cases, I'd suggest using Subversion
and TortoiseSVN as frontend.
*****
Actually, I'm surprised that it ever "worked", since nothing you've described here can
work.
****

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



Relevant Pages

  • Re: display message in work thread
    ... The method of posting to a UI window works for me, but you would obviously want to throttle the messages if you think they may come too quickly. ... The worker thread will gain its message queue, but you shouldn't worry about it, and after the message box is closed the queue will just exist there doing nothing. ... Nor should you post messages to main thread only so that main thread would pop up message boxes. ...
    (microsoft.public.vc.mfc)
  • Re: Dialog from Thread without message loop
    ... But you didn't really say if this was a worker thread or a UI thread. ... The thread doesnot have a message loop. ... "Any thread can create a window. ... message loop to process the messages in its message queue. ...
    (microsoft.public.vc.mfc)
  • Re: Implement a SendMessage behaviour
    ... I know the call stack of a thread can't be empty but it was the simpliest way I found to explain the feature I needed. ... I meant I need as less as possible results of user actions in call stack of the thread that will run the new action. ... As soon as the next action is started, that's not a problem if the windows message queue receive some message as they will wait my current action ends to be proceed ... not in any way guarantee that by the time the message sent is actually processed that the window message queue is empty. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Update GUI in worker thread?
    ... I would use PostMessage to send the HBITMAP to the window that will ... This way your worker thread is not blocked during the painting. ... PostMessage() that tells it what to update. ...
    (microsoft.public.vc.mfc)
  • Re: MFC and threads
    ... Ok there is a main gui window that creates a worker thread. ... // Some decision making happens and this worker thread code ... CMainGuiDlg dlg; << Primary window which launched Dummy ...
    (microsoft.public.vc.mfc)