Is it possible to use a buttoncontrol to pause a for loop.
Ex: When it has counted to 50000, I press Pause and when I press Pause again, the loop continues.
Is it possible to achieve something like this ?

for ( int i = 0; i < 100000; i++ )
{
	i = i + 1;
}

you can put something in an if statement

for ( int i = 0; i < 100000; i++ )
{
	i = i + 1;
          if( i == 50000 )
         {
             cout << "Press Enter to continue ...";
             cin.get();
          }
}

Thanks, I am not sure if that is the approach I need to have.
I will describe better what I am doing.

I have 2 buttoncontrols on a Form(button1 and button2).
Inside button1 I have this for loop. So when I press button1 the for loop starts.
Now I could if I want use button2 to "Pause" the process of the loop inside button1.
So button2 will works as a pause/continue button.
First press pauses and the second continues the for loop inside button1.

What could be the approach for something like this ?

you can put something in an if statement

for ( int i = 0; i < 100000; i++ )
{
	i = i + 1;
          if( i == 50000 )
         {
             cout << "Press Enter to continue ...";
             cin.get();
          }
}

The problem I see is that the loop will need to be in a different thread than the buttons so that the loop and the buttons can run independently of each other. If they are all in the same thread then you can't press either button while the loop is executing. Once you put the loop in another thread you can create a global flag to indicate when to execute and pause the loop

For the below to work, Button1 will set the value of the flag to true and Button2 will set it to false.

bool flag = false;
for(int i = 0; i < 1000000; i++)
{
    while( flag == true)
    {
        Sleep(1000); // one second delay
    }
    // do something here
}

I have tried to write this code but the compiler says that ´Sleep´ identifier not found.
Do I need to include something in order to use this ?
Then I wonder about the sleep method. I did red something about it on MSDN and am not sure if I understand that the Sleepmethod "pauses" the loop for a specified time and then continues or is it in this case Until flag is false again ?
I am not really sure what 1000 meens here practically.

while ( flag == true )
{
	Sleep(1000);
}

Thank you Ancient, I will try to experiment with this...

I have put all this code as below and it do Pauses the for loop when I press the button that pauses the loop and the CPU-usage also goes down to 0-5 % from 100%. So this works.

However when I want to press the Pausebutton again to ´continue´ the loop it is impossible because the cursor has changed to a "WaitCursor" and is thinking in eternity and I have to force to quit the application.
Am I doing right with this code below ?

Initialize PauseAction to false in the Form:

Form3(void)
{
	InitializeComponent();
	this->PauseAction = false;
}

Declaring PauseAction:

private: bool PauseAction;

Button that turns PauseAction true and false as you press it.
Default is false, First press make it true, press again make it false and so on:

int i = 0;
		 
 if ( PauseAction == false )
{
	 PauseAction = true;
	 i = 1;
}

 if ( PauseAction == true && i == 0)
{ 
	 PauseAction = false;
}

This is the for loop that pauses:

using namespace System::Threading;

for ( int i = 0; i < 1000000; i++ )
{

     while ( PauseAction == true )
     {
	Thread::Sleep(1000);
     }

//Reading from files and other stuff here
}
for ( int i = 0; i < 100000; i++ ){  i = i + 1;          if( i == 50000 )         {             cout << "Press Enter to continue ...";             cin.get();          }}for ( int i = 0; i < 100000; i++ )
{
    i = i + 1;
          if( i == 50000 )
         {
             cout << "Press Enter to continue ...";
             cin.get();
          }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Jennifer, in your loop, try this instead

using namespace System::Threading;

for ( int i = 0; i < 1000000; i++ )
{

     if ( PauseAction == true )
     {
	i--; //Decrement the counter, so the loop can still run
         continue; //Skip the rest of the loop to simulate it being paused
     }

//Reading from files and other stuff here
}

If you decrement the counter in your if statement, it will give you the same effect as keeping i at 500000 or w/e number you were looking for, because once it hits the continue statement, it will go back to the top of the loop, and increment it one.

Edited 6 Years Ago by LevyDee: n/a

Hah, I did not =(. But I think my solution would have worked well though!

I beg to differ.... Look carefully at the question...

This article has been dead for over six months. Start a new discussion instead.