Click to See Complete Forum and Search --> : GetMessage not responding when I am in another window
logorman
May 8th, 2009, 01:06 PM
I have a C++ Windows program that produces a window and performs a GetMessage() loop to change that window upon a mouse/keyboard event or an event that a file has changed. It works great except when I bring up another window to change the file my program is waiting on -- to test it. When I change the file in another window, my program does not react to this until I move my mouse back over my program window (i.e., I don't even have to click on that window to make it the focus). Is there some parameter I can set to have my program react immediately to events even though I'm in another window?
I've included the message loop part of my C++ code to show my intention.
/* Main message loop - respond to either a mouse/keyboard event, or file change */
while (GetMessage(&msg, NULL, 0, 0)) { // get windows messages
iRslt = checkFileChange (SPEECH_FILE); // iRslt =1 if SPEECH_FILE changed
if (iRslt == 1) {
// PostMessage( hWnd, WM_FILECHANGE, NULL, NULL ); // does nothing
msg.message = WM_FILECHANGE;
fprintf (fpOut, "MAIN LOOP: file change%d\n"); // for debugging
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CoUninitialize();
--
kirants
May 9th, 2009, 06:26 PM
What in your view is the way your program reacts when you say this? my program does not react to this ? What kind of reaction does the program produce ? It is not clear since all you have shown is the message pump and am sure your program has something more than that ?
logorman
May 11th, 2009, 07:17 AM
What is supposed to happen when a particular file changes is that the GetMessage loop (which is located in WinMain() and that I posted above) should detect that the file has changed and this event triggers a call to a function that does a bitblt(), which just changes the background of my program window. As I say, that part of the code works fine:
-- IF my focus is the program window, then my program window changes immediately when the file changes,
-- or IF my focus is another window, then the window changes ONLY when I slide the cursor over my program window after the file has already been changed.
If neither of these conditions exists, my program window does nothing.
I'm thinking there must be a flag or window parameter that allows an event to trigger a window change even if my focus (and cursor) are in another window. But I can't find any such parameter and I could be wrong in this guess.
kirants
May 11th, 2009, 02:03 PM
Windows optimizes paint operations. Please post all code. Otherwise, it will end up in a needless exchange of questions back and forth :)
Arjay
May 11th, 2009, 02:47 PM
Be sure to post the checkFileChange() code too.
logorman
May 14th, 2009, 04:23 PM
I've attached the code. There are 2 files: speechFileChange.cpp and display.cpp. The speechFileChange contains the GetMessage loop in WinMain that I think is causing me my grief. The display.cpp program handles creating the window and redrawing it when instructed from some flow in one of the functions in speechFileChange.
The purpose of the program is to detect when a file changes. This file, speechOut.wav holds the audio of some speech that I want to perform recognition on, using SAPI. But this program never gets to that point because the GetMessage() loop will not act upon my PostMessage (WM_FILECHANGE ...) event.
A description of the symptoms is as follows.
- When I do a regular windows event (mouse or keyboard) in my program window, the GetMessage loop catches it and does what I want -- goes to WM_CHAR in WndProc, where it posts a WM_FILECHANGE event, which causes a WM_RECOEVENT event, which initializes the SAPI, does speech recognition in ProcessRecoEvent() and ExecuteCommand(), and returns the recognized spoken command.
- The above works great IF the event that starts it off is a mouse or keyboard event. But I don't want that. I want it to recognize a file change. So, I have a checkFileChange() function at the bottom of file speechFileChange. This looks at the speechOut.wav file times to see if it has changed. This also works great.
- However in the GetMessage() loop, even after the file has changed and checkFileChange() has detected it, I try to send an event message, WM_FILECHANGE. But this is never sent. Instead, the program does nothing until I do some windows event such as mouse or keyboard upon my program window, in which case it proceeds to recognizes the file's speech and returns the result of this recognition as it should.
I found in msdn that GetMessage() "only does marshalling for system messages (those in the range 0 to (WM_USER-1)) ... otherwise you must do custom marshalling". Indeed my WM_FILECHANGE is above WM_USER. I don't know what is meant by "custom marshalling". But I have other tags in WndProc, such as WM_RECOEVENT, that are successfully used by PostMessage() and GetMessage().
So, my question is how can I successfully detect a file change and act on this event.
cosminflorea
May 15th, 2009, 08:05 AM
I think that you should check always if there are changes to your file. If your window is not active, it doesn't receive messages and your test doesn't take place. You may try this
while (true)
{
sleep(...);
iRslt = checkFileChange (SPEECH_FILE); // iRslt =1 if SPEECH_FILE changed
....
}
Alternatively, you may refactor your code to send the FILECHANGE message at the point where the change does occur and then to wait for this message in your messages loop (like you did).
logorman
May 15th, 2009, 01:39 PM
Your point is a good one that the message loop will not process the code within unless a message has been posted, so I changed the message loop to be similar to your suggestion -- now it checks for either a message event or a change of file,
/* Main message loop - respond to either a mouse/keyboard event, or file change */
while ((iChange = checkFileChange(SPEECH_FILE)) || GetMessage(&msg, NULL, 0, 0)) {
if (iChange == 1) {
PostMessage( hWnd, WM_FILECHANGE, NULL, NULL );
iChange = 0;
}
else Sleep (200);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Now this window does respond to system event messages, and it does recognize when my speech file has been changed. But, even though I post WM_FRAMECHANGE, it doesn't respond to it, perhaps because it is user-defined (of event number > WM_USER).
Arjay
May 15th, 2009, 02:19 PM
I think you always need to check for the file change AND GetMessage each time in the loop.
logorman
May 21st, 2009, 02:47 PM
Just to finish this thread, I'm sorry that I can't give a clean answer, but I decided to step up a couple levels in programming language and am now using Python and pkinter to solve my problem.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.