View Full Version : Message pump
Doomulation
January 23rd, 2004, 23:41
I was wondering because I've been trying to do it forever now...
how is the message pump done? The one that translates and dispatches messages from the applications message quene? Looking at a normal, non-MFC app, it looks like this:
while( PumpMessage(...) )
{
TranslateMessage(...);
DispatchMessage(...);
}
However, anytime I try to do a similar message pump - anywhere - anyhow, it just freezes the window. I don't know how to now make it this way. I've tried putting an additional thread; but to no avail. Usually, the message pump I use looks like this:
while (!bExit)
{
cpu();
CheckInput();
...
(code as shown above)
}
(Yeah, it's the chip8 emulator.)
And...besides that, I can't seem to create a window using windows api. The only possible way I found of doing it was using the CreateDialog macro. The CreateWindow always fails with ret code 1400.
What am I doing wrong here? I desperatly need to get the message pump working while not blocking the application's main window.
SculleatR
January 24th, 2004, 10:54
PumpMessage ? I'm sure ya mean PeekMessage.
Well, you can setup a WinUpdate function looking like this:
void WinUpdate() {
MSG msg;
while( PeekMessage(...) ) {
if( GetMessage(...) ) {
TranslateMessage(...);
DispatchMessage(...);
}
}
}
about your Win32 Window creation, check if all is correct (class registration,
WM_CREATE and WM_DESTROY done? etc.) or even check some tutorial.
btw, whats wrong with a Thread?
HANDLE hThread;
DWORD dwThreadID;
void Emulation() {
for(;;) {
cpu();
CheckInput();
}
}
void StartEmu() {
hThread = CreateThread( NULL, 0,
(LPTHREAD_START_ROUTINE)Emulation,
NULL, 0,
&dwThreadID);
}
..
Doomulation
January 26th, 2004, 13:24
PumpMessage ? I'm sure ya mean PeekMessage.
Typo. My mistake X_X
Well, you can setup a WinUpdate function looking like this:
void WinUpdate() {
MSG msg;
while( PeekMessage(...) ) {
if( GetMessage(...) ) {
TranslateMessage(...);
DispatchMessage(...);
}
}
}
Hmm well, I never tried that...but there's still oddify I cannot understand.
about your Win32 Window creation, check if all is correct (class registration,
WM_CREATE and WM_DESTROY done? etc.) or even check some tutorial.
Any specific messages that must be handled for the function to initialize correct?
btw, whats wrong with a Thread?
HANDLE hThread;
DWORD dwThreadID;
void Emulation() {
for(;;) {
cpu();
CheckInput();
}
}
void StartEmu() {
hThread = CreateThread( NULL, 0,
(LPTHREAD_START_ROUTINE)Emulation,
NULL, 0,
&dwThreadID);
}
..
Even so, infinite loops like those are slow, even on idle priority (they actually keep disturbing other threads.)
I cannot quite understand how this message pump works...I've tried building a new project (win32 app) and it doesn't stall, or deadlock. I even used the exactly same as before:
while (!bExit) {
if (PeekMessage(...) {
TranslateMessage(...);
DispatchMessage(...);
}
EmulationThread();
}
It works perfecly fine, in other words! This is the oddest...but I wanna know really how to do processes like these that does not STALL other threads. Like having a code threads while the gui remains unharmed and accessible at any time.
Teamz
January 26th, 2004, 15:31
look for WaitMessage() in MSDN
tooie
January 27th, 2004, 15:34
ok doom ..
GetMessage .. will wait in idel state till there is a message .. that is why it is used. It allows the best useage CPU wise .. most emulators will have the emulation thread and a gui thread .. the gui thread will process the messages.
you can have the processing in one thread
for (;;) {
if (PeekMessage(...) {
TranslateMessage(...);
DispatchMessage(...);
}
Do some emu stuff like 100 cycles
}
my suggestion is go with the two threads ..
Doomulation
January 28th, 2004, 09:28
Indeed, I thought of it before, but to the current emulator - it is by far not necessary. Since no timing is emulated, I actually have to slow down the cpu to get correct speed.
gamefreaks
January 31st, 2004, 05:28
You need a Window Procedure to receive the messages.
The DispatchMessage() function invokes a callback on the WndProc() with the message.
ingonab
February 3rd, 2004, 08:21
However, anytime I try to do a similar message pump - anywhere - anyhow, it just freezes the window. I don't know how to now make it this way. I've tried putting an additional thread; but to no avail. Usually, the message pump I use looks like this:
What am I doing wrong here? I desperatly need to get the message pump working while not blocking the application's main window.
What's the purpose of putting that "message pump" loop "anywhere"? You can't just put it anywhere.. Vital functions like grabbing windows messages off the message queue need to be placed at standard or structured locations. I fear you may be taking bits and pieces of code from here and there and putting it all together without really understanding the flow of your overall code. Understanding code flow is immensly important especially if you are using multiple threads.
And...besides that, I can't seem to create a window using windows api. The only possible way I found of doing it was using the CreateDialog macro. The CreateWindow always fails with ret code 1400.
What does your CreateWindow(...) call look like?
Doomulation
February 3rd, 2004, 12:56
What's the purpose of putting that "message pump" loop "anywhere"? You can't just put it anywhere.. Vital functions like grabbing windows messages off the message queue need to be placed at standard or structured locations. I fear you may be taking bits and pieces of code from here and there and putting it all together without really understanding the flow of your overall code. Understanding code flow is immensly important especially if you are using multiple threads.
Indeed, I understand it. I just want to know the mechanism it uses to not block the window. Because usually loops like these WILL block the main window.
What does your CreateWindow(...) call look like?
You know...I think I've since long forsakened that code. I used a new win32 project to get it right. The message loop, too.
ingonab
February 3rd, 2004, 14:14
Indeed, I understand it. I just want to know the mechanism it uses to not block the window. Because usually loops like these WILL block the main window.
Normally it doesn't block the main window because the window is controlled from inside the loop.
Doomulation
February 4th, 2004, 12:03
How so? The translate/dispatch messages?
My custom such loop did not work at all before I made a win32 project.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.