-
June 16th, 2013, 05:47 PM
#1
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?
-
June 16th, 2013, 06:08 PM
#2
Re: Exiting application during while loop operation
Originally Posted by frahman
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.
-
June 16th, 2013, 08:24 PM
#3
Re: Exiting application during while loop operation
Originally Posted by GCDEF
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.
-
June 17th, 2013, 01:39 AM
#4
Re: Exiting application during while loop operation
Originally Posted by frahman
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
-
June 17th, 2013, 02:59 AM
#5
Re: Exiting application during while loop operation
Originally Posted by frahman
... 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
-
June 17th, 2013, 06:09 AM
#6
Re: Exiting application during while loop operation
Originally Posted by frahman
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:- Moving lengthy operation to a worker thread (Win32 style, exactly what Victor said about in the message above)
- Pumping messages on every iteration (Win16 style)
Best regards,
Igor
-
June 18th, 2013, 06:50 AM
#7
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.
Originally Posted by VictorN
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
-
June 18th, 2013, 06:52 AM
#8
Re: Exiting application during while loop operation
Thanks, Igor. I'll try implementing the worker thread approach.
-
June 18th, 2013, 07:04 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|