大家,嘿 我想实现一个按钮,按下它时会做某事。
想法是在单击按钮(MouseDown)时启动一个线程,并在释放按钮时停止它(MouseUp)。
事件处理程序:
this->button18->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &Demo::button18_MouseUp);
this->button18->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Demo::button18_MouseDown);
事件:
private: System::Void button18_MouseUp(System::Object^ sender, MouseEventArgs^ e)
{
stop = true;
button18_Thread->Join();
}//end of button18_MouseUp
private: System::Void button18_MouseDown(System::Object^ sender, MouseEventArgs^ e)
{
stop = false;
button18_Thread = gcnew Thread(my_move_Thread);
button18_Thread->Start();
}//end of button18_MouseDown
线程解除和功能:
ThreadStart^ my_move_Thread = gcnew ThreadStart(this, &Demo::move);
Thread^ button18_Thread;
void move()
{
while (!stop)
{
label17->Text = Convert::ToString(--act_vel);
}//end of while
}//end of move
有谁能告诉我为什么程序以循环结束?
答案 0 :(得分:0)
而不是布尔停止和Thread-> Join(),我现在只使用Thread-> Abort()并且它可以工作。
我仍然想知道为什么Thread-> Join()以循环结束。