Thread: exit while loop when button clicked

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    exit while loop when button clicked

    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
    }

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Hmmm. Are you just starting windows programming?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Yeah, just starting to learn MFC.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    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

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You could just add it to your windows message loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    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

  12. #12
    Trolley boy JackGL's Avatar
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Quote Originally Posted by shav
    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.
    First off... reading the source for a client probably won't help you much when you're writing a server.

    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:

    Code:
        while (1) {
            AcceptSocket = SOCKET_ERROR;
            while ( AcceptSocket == SOCKET_ERROR ) {
                AcceptSocket = accept( m_socket, NULL, NULL );
            }
            printf( "Client Connected.\n");
            m_socket = AcceptSocket; 
            break;
        }
    That basically keeps trying to connect until AcceptSocket doesn't equal SOCKET_ERROR.

    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.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    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

  14. #14
    Trolley boy JackGL's Avatar
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    To be honest, I've never used MFC.

    You could, I suppose, use a loop like this:

    Code:
    while ( busy == false ) {
    
    }
    Then have it so that when the button is pressed busy =! busy;.

    Maybe. Possibly.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Yeah, that's pretty much what

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My BS_OWNERDRAW button draw as white when clicked...
    By Jonathan Beaubi in forum Windows Programming
    Replies: 1
    Last Post: 08-16-2007, 10:41 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM