CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2013
    Posts
    4

    Exiting application during while loop operation

    Hi guys,

    I have a VC++ 6 dialog-based application. This application is intended for reading data from a USB device (this is a FTDI FT245R chip). I have a start button on the dialog. On clicking it, unsigned char data retrieved from the USB device is displayed on the dialog. The start button invokes a function which includes a while (1) loop inside which the USB device is read and the values displayed. There are no break or continue statements inside the always operating while (1) loop. All this works fine. My problem is how do I exit the application as normally it would just run forever? I have put an exit button on the dialog with code added to call OnOK(). Normally this would nicely close the application but as I have an endless while loop in my application so this button doesn't seem to work. Nothing happens on clicking it and clicking it repeatedly just hangs the program. What to do?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Exiting application during while loop operation

    Quote Originally Posted by frahman View Post
    Hi guys,

    I have a VC++ 6 dialog-based application. This application is intended for reading data from a USB device (this is a FTDI FT245R chip). I have a start button on the dialog. On clicking it, unsigned char data retrieved from the USB device is displayed on the dialog. The start button invokes a function which includes a while (1) loop inside which the USB device is read and the values displayed. There are no break or continue statements inside the always operating while (1) loop. All this works fine. My problem is how do I exit the application as normally it would just run forever? I have put an exit button on the dialog with code added to call OnOK(). Normally this would nicely close the application but as I have an endless while loop in my application so this button doesn't seem to work. Nothing happens on clicking it and clicking it repeatedly just hangs the program. What to do?
    Use a boolean variable to control the loop. Set it to false when the user takes an action that would close the app.

  3. #3
    Join Date
    Jun 2013
    Posts
    4

    Re: Exiting application during while loop operation

    Quote Originally Posted by GCDEF View Post
    Use a boolean variable to control the loop. Set it to false when the user takes an action that would close the app.
    Thank you for the suggestion. I tried that but it didn't work. I don't know how I can change the boolean variable inside the loop as the only processing that takes place there is reading USB values. While the loop is operating it appears that any externally generated messages are simply ignored. I was expecting that invoking OnOK() would exit the loop and terminate the application but it doesn't. OnOK() does work if I don't have a while (1) loop and just use the reading code once every time the user clicks a button. Clearly putting code inside a while (1) loop with no loop termination condition built in is resulting in it operating forever with no apparent way of closing the program.

    I can post the code (it is quite short) if that would help.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Exiting application during while loop operation

    Quote Originally Posted by frahman View Post
    I tried that but it didn't work. I don't know how I can change the boolean variable inside the loop
    You either don't know how, or you tried that. Those are mutual exclusive things, you know. So what did really take place? You tried to know how?
    Best regards,
    Igor

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Exiting application during while loop operation

    Quote Originally Posted by frahman View Post
    ... The start button invokes a function which includes a while (1) loop inside which the USB device is read and the values displayed. There are no break or continue statements inside the always operating while (1) loop. All this works fine. My problem is how do I exit the application as normally it would just run forever?
    You should implement a worker thread and move your lengthy calculations/reading into the thread. then your UI would response to any user actions (like press OK/Cancel buttons and so on.)
    Have a look at this essay: Using Worker Threads
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Exiting application during while loop operation

    Quote Originally Posted by frahman View Post
    I have a start button on the dialog. On clicking it, unsigned char data retrieved from the USB device is displayed on the dialog. The start button invokes a function which includes a while (1) loop inside which the USB device is read and the values displayed. There are no break or continue statements inside the always operating while (1) loop. All this works fine.
    It cannot be fine as long as your GUI does not react to user input. You have to provide GUI responsiveness while looping. Basically this can be done two ways:
    1. Moving lengthy operation to a worker thread (Win32 style, exactly what Victor said about in the message above)
    2. Pumping messages on every iteration (Win16 style)
    Best regards,
    Igor

  7. #7
    Join Date
    Jun 2013
    Posts
    4

    Re: Exiting application during while loop operation

    Thank you - I am taking a look at the Worker Thread technique. It certainly looks like it will solve the problem.

    Quote Originally Posted by VictorN View Post
    You should implement a worker thread and move your lengthy calculations/reading into the thread. then your UI would response to any user actions (like press OK/Cancel buttons and so on.)
    Have a look at this essay: Using Worker Threads

  8. #8
    Join Date
    Jun 2013
    Posts
    4

    Re: Exiting application during while loop operation

    Thanks, Igor. I'll try implementing the worker thread approach.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Exiting application during while loop operation

    Most of the posters here advocate threads, but in simple cases like this, a message pump is easier and just about as effective. Add these lines to your loop

    Code:
    	MSG msg;
    	while(PeekMessage(&msg, GetSafeHwnd(), 0, 0, PM_REMOVE))
    		DispatchMessage(&msg);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured