Exit a Infinite for Loop

Hi ,

I want to read some data of an USB Data Acquisition System every 5 milliseconds.
I have created a infinite for loop that reads every 5ms data.
I start the loop with a button (I use MFC) , but when I want to exit the loop with a flag that I want to enable with a stop button, it is not possible to click on some thing.

Can you help me.
Where else should I put my code te read data with an infinite loop? (Now it is under a start button, sow when start button is cliked it stays clicked and I can not click some thing else)
Should I use a flag and how ?

I made my program with visualC++ as an MFC application (CFormView), is there a place in the code that is continously checked so that I can put my infinite loop there with a start and stop flag. (and not under the start button)

thanks a lot
Pressing the button should start a timer. You should perform your sample on receipt of WM_TIMER. The timer is inaccurate and can slow down under system load.

It sounds like you've added your code in the button-clicked handler.
closed account (z05DSL3A)
A timer may be a better alternative to an infinite loop.

http://msdn.microsoft.com/en-us/library/ms644906.aspx

Edit: I should refresh before posting
Last edited on
I have chosen the QueryPerformance-functions because they are very accurate, and indeed I putted the code in the button-clicked handler. :

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);
for(int i=0;stop==0;)
{
QueryPerformanceCounter(&t2);
if((teller*(Speed/1000)*frequency.QuadPart)<(t2.QuadPart - t1.QuadPart))
{
ElapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
read data................... } }


I now that the code is not on the right place under the button-clicked handler but I dont now where else.

void CRemProefView::OnBnClickedButtonStart()
{

stop=0;
ReadData() ; // start infinite for loop
return;
}

void CRemProefView::OnBnClickedButtonStop()
{
// TODO: Add your control notification handler code here
stop=1;
return;
}

Topic archived. No new replies allowed.