15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CWinThread::PostMessage looses messages??

Jeff Wishnie -- jwishnie@swellsoft.com
Wednesday, October 02, 1996

Environment: VC++ 4.2, Win 95

I think I've found a bug where the message queue LOOSES messages. Here's my
situation:

In response to certain events in my app, I want to post a message into my
app's message queue so that I can return immediately and finish processing
later (after the current event is handled).

NOTE: this is a _single_ threaded app.

So, I do the following:

...
AfxGetApp()->PostThreadMessage(MY_CUSTOM_MESSAGE, NULL, NULL)
...

and in my app subclass:

void
MyApp::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message==MY_CUSTOM_MESSAGE) {
        finishProcessing();
        return TRUE;
    }

   return CWinApp::PreTranslateMessage(pMsg);
}

According to the docs, this should work fine.

Problem is, I don't receive every message I post!

To test this I sent messages with a monotonically increasing id as such:

static UINT _id=0; // file static
...
AfxGetApp()->PostThreadMessage(MY_CUSTOM_MESSAGE, NULL, _id++)
...

and in my app subclass:

void
MyApp::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message==MY_CUSTOM_MESSAGE) {
        TRACE("Got message id: %d\n", (UINT) pMsg->lParam;
        return TRUE;
    }

   return CWinApp::PreTranslateMessage(pMsg);
}

And this is my output (paraphrased):

Got message id: 0
Got message id: 1
Got message id: 2
Got message id: 3
Got message id: 5 <--- WHAT HAPPENED TO 4?

Basically, every now and then, with no predictable pattern, the queue looses
messages AND I _really_ need to get them all! When I miss them, my app gets
out of synch and dies none-to-nicely.

Is this a BUG or am I missing some important "Give me all messages I post"
flag??

Thanks,

Jeff

jwishnie@swellsoft.com
415 243-9900 (w)




David Lowndes -- David.Lowndes@bj.co.uk
Friday, October 04, 1996

[Mini-digest: 4 responses]

>I think I've found a bug where the message queue LOOSES messages. Here's my
>situation:

Jeff,

Have you checked the return value from PostThreadMessage to see if it
returns an error?

Also you don't say when you're posting the message.

I had a problem under Windows 3.x with posting DDE requests during
WM_ACTIVATE (or was it WM_SETFOCUS?) processing. The DDE message
was posted to the server application (with no error from PostMessage), but
the server never saw it. However that's not the same as your problem as I
solved mine by using the same technique that you're having problems with.

Dave Lowndes
-----From: Koronthaly David 

I had a simmilar problem while posting messages in a loop.
The story is, that message queue can overflow - if you will not read
from it.

You should test the return value from PostMessage

David Koronthaly
Intergraph SR

-----From: Noel Burton-Krahn 

See my earlier message, "Lost thread messages".  I believe thread messages 
are lost when someone else (like a dialog box or CRectTracker) calls
	GetMessage(&msg, NULL, ...)
	DispatchMessage(&msg)

As far as I can tell, DispatchMessage throws away messages whose hWnd's are 
NULL (ie thread messages).  This is a real oversight, and renders thread 
messages useless for any thread that ever causes another message loop to be 
invoked.  I noticed this bug when I resized my main app window.

--Noel


-----From: Marty Fried 

I don't have VC 4.2 yet, so I apologise if this is incorrect - I'm assuming
PostThreadMessage is similar to the Win32 function, and has a return value.
The docs for posting messages urge us to check for success and not assume
every post succeeds - the queue could be full.  Have you tried checking the
return value to see if the call succeeded?  Also, is it possible you are
using a message number that something else may be getting?
________________________________
 Marty Fried (mfried@linex.com)
 Marin County, California






| Вернуться в корень Архива |