Hi all,
is it possible to remain in a while loop until a button is clicked. I want to do something like below
while (button not clicked)
{
do looping stuff
}
Printable View
Hi all,
is it possible to remain in a while loop until a button is clicked. I want to do something like below
while (button not clicked)
{
do looping stuff
}
Hmmm. Are you just starting windows programming?
Yeah, just starting to learn MFC.
Well, you could have your button set a timer and then when the timer event fired you could perform your code. When the button got pressed again you would then kill the timer. Timer's aren't all that reliable though as in they have low precedence in the big scheme of things.
You could also have a global variable that your button changed based on it's state. Then in your message loop you could check the value of the variable and then based on the outcome of the comparison either perform or not perform your section of code.
Once you get more into Windows programming you could create an additional process to perform your section of code and then kill the process when the button was pressed again.
Thanks for the reply.
How do I edit my code in MFC so that when the application starts up it goes straight into the loop. Then when, a button is clicked it changes the global variable you mentioned and will break of out that loop
I've tried by calling the loop function in the initinstance but no joy. Any ideas?
Thanks
Shav
>>> do looping stuff
If your program is locked in that loop, then Windows will not be able to process the message that the button was pressed. If you have a busy loop, you should run it in a seperate thread. That way your UI will still be active.
You could just add it to your windows message loop.
Yes, he could peek the message queue looking for the button click. It would, of course, slow the windows loop down, depends what it is really.
Sounds like a dodgy design.
Actually that was in response to changing a global variable and then in the message loop check for the state of the variable and then if it pressed do loop else don't. Doesn't really slow much down as in most games are set up with a similar message loop, aka checking for WM_QUIT.
Prahaps if you describe the task in more detail we will be able to come up wit a better design.
Or try..
>>How do I edit my code in MFC so that when the application starts up it goes straight into the loop.
Add a handler for WM_CREATE or modify the main apps InitInstance() to call loop.
>>Yeah, just starting to learn MFC.
So finding and modifing the msg que in an MFC app is probably in the 'ToHard' basket at present (as is multi-threading which seems the best way to do this)
Bring us back to checking in the while loop for the button click.
IMHO this will cause performance issues though.....
I would call this last in your while loop (so just before the while tests again)
call PeekMessage() with PM_NOREMOVE to see if any messages for your app but not removing them from the que.
Filtering the range of msgs 'peeked' may improve performance.
When a message is found check the returned MSG (msg) struct for;
msg.message == WM_COMMAND // message to a control
LOWORD(msg.wParam) == ID_BUTTON //for the button
HIWORD(msg.wParam) == BN_CLICKED //button clicked
exit the while loop and the button click will be processed (or now call PeekMessage() and remove it)
the LOWORD and HIWORD macros come from Windows.h
Ok people, thanks all for the responses.
Basically what I'm trying to do is write a program in MFC that when it starts up listens on a socket for any incoming packets. This is why I was suggesting to use a while loop. Now when a button is pressed, it is possible for an end user to send data. Hence, why I wanted to break out of the listening while loop, transmit data and then re-enter the loop.
I'm using the MSDN winsock2 example http://msdn.microsoft.com/library/de...lient_code.asp as the basis for my receive & transmit. Unfortunately, I don't know much about MFC. Thanks
Shav
First off... reading the source for a client probably won't help you much when you're writing a server. :pQuote:
Originally Posted by shav
Take a gander at this.
The socket is put into a state called listen(), which basically does exactly what you're looking for. It listens for incoming transmissions.
Then there is this sneaky loop:
That basically keeps trying to connect until AcceptSocket doesn't equal SOCKET_ERROR.Code:while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.\n");
m_socket = AcceptSocket;
break;
}
Hope this helps. I'm also in the process of writing a client and server application, so we're both in pretty much the same boat. :)
Hope this helps.
Thanks for reply JackGL. 2 things
1) I was using recvbuf from the client code and was able to get input. What is the benefit of using this method
2) How can I implement this while loop in MFC and be able to break from it when a button is pressed and return to it when an action is completed, ie sending data. Basically I want my program to be always listening on socket until it has to send something
Cheers
Shav
To be honest, I've never used MFC.
You could, I suppose, use a loop like this:
Then have it so that when the button is pressed busy =! busy;.Code:while ( busy == false ) {
}
Maybe. Possibly.
Yeah, that's pretty much what