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?
Re: Exiting application during while loop operation
Quote:
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.
Re: Exiting application during while loop operation
Quote:
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.
Re: Exiting application during while loop operation
Quote:
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? :D
Re: Exiting application during while loop operation
Quote:
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
Re: Exiting application during while loop operation
Quote:
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)
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
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
Re: Exiting application during while loop operation
Thanks, Igor. I'll try implementing the worker thread approach.
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);