Two button clicks needed for event handler to work

Forum - I have a small C++ app in which it takes two clicks of a button to produce the desired even handler action.

The app controls a voltage digitizer over the USB and plots the measured result in a pictureBox control. The app GUI has several Button controls, two of which are Run and Stop. Run_btn_Click starts the measurement and plotting (up to 600 data points) using a for-loop; Stop_btn_Click is intended to interrupt the action of Run. Both are button event handlers. The problem is that it takes two clicks of the Stop button to interrupt the Run button event handler. Either a doubleclick or two successive single clicks will work; time between single clicks doesn't seem to matter.

The following code excerpt is a condensed version of Run_btn_Click:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private: System::Void Run_btn_Click(System::Object^  sender, System::EventArgs^  e) {
	//Variable definitions, pictureBox setup, etc.

			 A = false;

			// For-loop
			 for(K = 0; K <600 && A == false; K++)
			 {
			 Application::DoEvents();
			 OutBuffer[0] = 0x80; //Hex 0x80h is command to send "operate/do something" code to device.
			 WinUsb_WritePipe(MyWinUSBInterfaceHandle, 0x01, &OutBuffer[0], 64, &BytesWritten, NULL);
			 WinUsb_ReadPipe(MyWinUSBInterfaceHandle, 0x81, &InBuffer[0], 64, &BytesRead, NULL);
			 
			 DATAH = (InBuffer[0] << 8);
			 DATAL = InBuffer[1];
			 DATA = DATAH + DATAL;
			 NUMF = (DATA*ADV)/1024;
			 textBox2->Text = Convert::ToString(NUMF);
			 listBox1->Items->Add(NUMF);
			 g->DrawLine(blackPen, Point(J,(400*(1-NUMF/FSV))), Point(J+1,(400*(1-NUMF/FSV))));
			 this->textBox2->Update();
			 this->listBox1->Update();
			 this->pictureBox1->Update();

			 J = J + 1;
			 I = I + 1;
			 Tmr->Interval = TI;
			 Tmr->Enabled = true;
			 GC::KeepAlive(Tmr);
			 while(Tmr->Enabled==true)
				 {
					;
				 }
			 Tmr->Enabled=false;
			 } // End of for loop
			 return;
} // End of Run_btn_Click 


Here's the code for Stop_btn_Click:

1
2
3
private: System::Void Stop_btn_Click(System::Object^  sender, System::EventArgs^  e) {
		A = true;
	} // End of Stop_btn_Click 


Problem: while the for-loop is running, click Stop button once, nothing happens; click Stop button a second time, loop breaks, app is ready for another Run button click.

Any ideas why this second click is required and what should be done to make the app respond to the first Stop_btn Click?

Thanks for any help that can be offered.

Mick H
4 Sept 20
2:09 PM PDT USA
Topic archived. No new replies allowed.