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

Edited 6 Years Ago by EricM3: n/a

I think you will find this easier to do if you start-up a worker thread to perform your "process" leaving the original thread running the user interface.

My C++/CLR is quite rusty but I think you could use a delegate to call back to the interface thread with process updates to display and you could easily use an interprocess communications object such as an event to communicate a stop event to the background worker thread.

See System.Threading in the .NET documentation

Comments

Thanks for the reference, Banfa.
I am trying to debug, but I can't seem to get my text box to refresh...

Form2 ^fm = gcnew Form2;
fm->ShowDialog();
for (int i = 0; i < 5; i++)
{
fm->richTextBox1->Text = fm->richTextBox1->Text + Convert::ToString(i);
//Application::DoEvents();
fm->richTextBox1->Refresh();
}

The output should be something like:

01234

But I have tried every combination of "Refresh" and "DoEvents" and it won't update.


*********
Also, I have tried the threading idea that you gave me, Banfa, but I can't figure out how to reference the same "Form2 ^fm" in both functions...

I have this...

static void ThreadProc()
{
  // Do Stuff 1
  Thread::Sleep(0);
}

and...

Thread^ oThread = gcnew Thread(gcnew ThreadStart(&Form1::ThreadProc));
Thread->Start();
// Do Stuff 2
Thread::Sleep(0);

So I thought I would "ShowDialog" in "Do Stuff 2". Then, perform my actions and update the text box in "Do Stuff 1" And end when "Form2 ^fm" is closed, but I'm having trouble referring to "fm" in both functions.

Edited 6 Years Ago by EricM3: n/a

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