【发布时间】:2020-03-15 00:12:18
【问题描述】:
我在标签点击事件中启动了一个线程。线程完成了它的工作。我什至得到了线程内的最后一个消息框。完成线程后,我想在标签单击事件中执行更多指令。所以我使用了if 语句和线程的IsAlive 属性。似乎线程的IsAlive 属性值始终为true。如何?如何确保我关闭应用程序时所有线程都会结束,或者当我关闭应用程序时它会自然结束?
private void lbl_Calc_Click(object sender, EventArgs e)
{
Label label = (Label)sender;
//new thread to do the calc
Thread t = new Thread(() => ThreadedMethodForCalc(label));
t.Start();
//checking the IsAlive property
//but doesn't work
if (t.IsAlive==false)
{
MessageBox.Show("please enter the data");
//more instruction here
}
}
private void ThreadedMethodForCalc(Label label)
{
//calculation. not shown
MessageBox.Show("end of thread"); //executed
return;
}
【问题讨论】:
-
您意识到您的检查是在您启动线程后立即执行的,而不是在它完成时?
-
@GSerg 但是我没有 ´´´MessageBox.Show("please enter the data"); '''消息。
标签: c# multithreading