Skip to main content
gamedev.net
🔒 Locked

function looping in windows

Started by etsuja May 22, 2006 at 6:16 PM 11 replies 1.2k views
Original Post
etsuja
etsuja
How do I make a function constantly loop while a windows program is running without ignoring the windows message loop?
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
LessBread
LessBread
Don't forget to craft a means to break the loop.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
etsuja
etsuja
is there a way besides threads?
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Falken42
Falken42
You could poll using PeekMessage, instead of using GetMessage which blocks. If a message exists, then Translate and Dispatch it.

No threads required.
etsuja
etsuja
I'm trying to do this and it's not working

do	{		    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		    {			if (msg.message == WM_QUIT) break;			TranslateMessage(&msg);			DispatchMessage(&msg);						    }	            SendRcv();	}	while(TRUE);


doesn't seem to be accepting any messages. I can't click any menu items.
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Falken42
Falken42
Then something in your SendRcv() function is blocking.
etsuja
etsuja
Then I guess this method won't work for me... all that's in that function right now is a recvfrom() function.
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Falken42
Falken42
Windows Sockets are set to default to blocking unless you explicitly disable blocking with ioctlsocket and the FIONBIO argument. You'll have to then properly handle the WSAEWOULDBLOCK return value from recvfrom.

Writing code which polls everything (sockets, messages, objects, etc) is a lot harder to manage. Putting your network code on a separate thread while keeping the message pump in blocking mode on the main thread would probably be the most ideal situation.
etsuja
etsuja
Couldn't I just use OnIdle()?
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Falken42
Falken42
Sure, if you're using MFC.

But you can't have any blocking calls within OnIdle. And OnIdle is only called when the application's message queue is empty.
etsuja
etsuja
K, well I guess I'm gonna be forced to use threads, I've never used them before though so I have another question that I don't see answered on msdn. how do you check to see if a thread is running?

EDIT:nevermind, I was gonna use the loop previously mentioned and create a new thread, check to see if it was running and if not destroy it every iteration but now I'm just creating a thread at initialisation that runs a constantly looped function. I just hope I can get away with running unsychronized threads.

[Edited by - etsuja on May 22, 2006 11:23:05 PM]
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
Falken42
Falken42
You can test if a thread is still running or not with WaitForSingleObject. Pass it the handle of the thread and a timeout of zero. If the thread is still running it returns WAIT_OBJECT_0, I believe.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.