stop button not working

closed account (4w7X92yv)
I got a form with a start and stop button, when i press starts it executes my code, but when i stop it doesnt stop it
What do i do wrong?


in form1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->demoThread =
                gcnew Thread(gcnew ThreadStart(this,&Form1::doTheWork));

				this->demoThread->Start();
				 btnStart->Enabled = false;
				 btnstop->Enabled = true;

				 lblText->Text = "Program started";
				 lstStatus->Items->Clear();
				 lstStatus->Items->Insert(0,"Program started");
				bool terminate = false;
			 }

private: System::Void btnstop_Click(System::Object^  sender, System::EventArgs^  e) {

				lstStatus->Items->Clear();
				lstStatus->Items->Insert(0,"Program stopped");
				lblText->Text = "Program stopped";
				 btnstop->Enabled = false;
				 btnStart->Enabled = true;
				bool terminate = true;


in main

1
2
3
4
5
6
7
8
int myMain(System::Windows::Forms::ListBox^ lstStatus)
{  do
{

// the code that is executed

}while (terminate == false);
}


when i press the start it places terminate on false and executes my code.
when i press stop it places terminate on true but still executes my code,
how do i get out of the thread by using the stop button
I don't know C++/CLI language, but the idea is this:
In your worker thread procedure (I think Form1::doTheWork) check the bool variable terminate every time you can and return when becomes false. There are better ways to do it, but keep it that way for now.

The variable must be accesible from both threads, in your code I don't think it is.
Topic archived. No new replies allowed.