Hello everyone,
I am writing a program in Visual C++, using VS 2008.
This is what I want:
I press a button, which begins an infinite loop, that does something (approximately) once per second until the user tells it to stop.
The problem I am having is finding a way for the user to stop. I don't really care how it is stopped (it can be a button press, or press any keyboard key, for example)
Here is my code:
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
// ....
// There is some error checking and initialization stuff here,
// ....
while(1==1)
{
if(timeflag == 0) // If flag not set...
{
time1 = clock();// Get a value of the clock
timeflag = 1; // Set the flag
}
time2 = clock(); // Get a second value of the clock
difference = difftime(time2, time1); // time2 - time1
if(difference >= CLOCKS_PER_SEC)// If diff >= 1 sec...
{
for(int i = 0; i < desired_frequency; i++) // Loop "desired_frequency" times
{
// PERFORM OPERATIONS HERE
}
clock_box->richTextBox1->Text = "Text"; // Write
clock_box->richTextBox1->Refresh(); // Refresh box
timeflag = 0; // Reset flag
}
// ???? Now check some condition to see if we should exit
} // End of loop
}
********
Also, while I'm here, I may as well describe my other problem, although it's not a big deal becaus I can probably figure it out with a little reading.
When the button is pressed, I would like to open some status box that updates every second. So, each time the "PROCESS" is executed, I will add a string to the status box confiriming that the operation was successful.
Right now, I basically have made a new form with a Rich Text Box, and I am trying to display the form as soon as the button is pressed, and refresh it as the while loop runs. I THINK that what happens is the form opens and the code pauses until the form is closed, but I'm not sure. I might post the code if anyone wants to help, but I'm more concerned with the problem I mentioned earlier.
Sorry this message is long.
Thanks.
Eric