Discussion:
Windows Message Loop + 'select'
(too old to reply)
m***@gmail.com
2018-05-28 11:27:38 UTC
Permalink
Hello,

Question related to windows message loop:

{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

This loop is required to be executed to handle windows events.

How to integrate into this loop 'select' function that waits for example for client on server socket.

I see one solution which I don't want to use (due to synchronization issues):
{
THREAD A { Windows Message LOOP }
THREAD B { Select }
}

Is there any possibility to integrate to the original Windows Message LOOP
a function, callback or something like that:
o) MessageSelect

/* ++++ ========================================================== */

int MessageSelect(void (*)(callback)(void *, int), void *,
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout
);

fd_set set;

void select_callback(void * selectHandle, void * user_data, int select_result)
{
printf("Select result = %d\n", select_result);

/* Process select event -> no issue with synchronization - this is called from main thread */
}
...
SOCKET sock;
...
void main(void)
{
FD_ZERO(&set);
FD_SET(sock, &set);

MessageSelect(&select_callback, "a user data", sock + 1, &set, NULL, NULL, NULL);

{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
...
}

/* ---- ========================================================== */

Regards
--
Maciej Labanowicz
R.Wieser
2018-05-28 12:16:45 UTC
Permalink
Maciej,
Post by m***@gmail.com
How to integrate into this loop 'select' function that waits for example
for client on server socket.
You don't. What you need to do is to use 'MsgWaitForMultipleObjectsEx' (
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684245%28v=vs.85%29.aspx )
in the messageloop to check for a signalled socket, and take it from there.

Warning: Will work with classic message loops, but not - as far as I can
tell - with the ones in dialogs.

Regards,
Rudy Wieser
Kaz Kylheku
2018-05-28 18:27:12 UTC
Permalink
Post by m***@gmail.com
Hello,
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
This loop is required to be executed to handle windows events.
How to integrate into this loop 'select' function that waits for example for client on server socket.
{
THREAD A { Windows Message LOOP }
THREAD B { Select }
}
If you don't wan threads, you can use select in a polling manner by
using a zero timeout.

To avoid blocking on GetMessage, you can use PeekMessage. So in your
loop, you check the queue with PeekMessage, check sockets with a
zero-timeout select() call and sleep for a little while.

There is another way. Set up timer using SetTimer to post a periodic
WM_TIMER message to the window handle. In the WM_TIMER handler, do your
polling select.
Charlie Gibbs
2018-05-28 23:28:28 UTC
Permalink
Post by Kaz Kylheku
Post by m***@gmail.com
Hello,
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
This loop is required to be executed to handle windows events.
How to integrate into this loop 'select' function that waits for example
for client on server socket.
{
THREAD A { Windows Message LOOP }
THREAD B { Select }
}
If you don't wan threads, you can use select in a polling manner by
using a zero timeout.
To avoid blocking on GetMessage, you can use PeekMessage. So in your
loop, you check the queue with PeekMessage, check sockets with a
zero-timeout select() call and sleep for a little while.
There is another way. Set up timer using SetTimer to post a periodic
WM_TIMER message to the window handle. In the WM_TIMER handler, do your
polling select.
I basically use that last method, and it works just fine.

However, we have one customer site where messages stop coming for some
unknown reason. They do have some other funky software running on their
machines, and maybe it's eating messages destined for me - not just
WM_TIMER, but everything. As a result, I see nothing after a while,
and my program becomes unresponsive. For them, I've added an option
that makes my program use your first solution instead; it works well
too, although I prefer the WM_TIMER technique.
--
/~\ ***@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ Fight low-contrast text in web pages! http://contrastrebellion.com
Loading...