public partial class MainWindow : window
{
private Thread t = new Thread;
private void btnSend_Click(object sender, RoutedEventArgs e)
{
if (t != null)
{
if (t.IsAlive == true)
{
t.Abort();
t = null; //Is this correct? should I free this before making null?
return;
}
t = new Thread(send.Image);
t.Start();
}
}
}
上面的代码显示了一个事件处理程序。当我按下一个名为“发送”的按钮时,应创建新的过程。当我点击同一个按钮时,程序应该停止。然后我再次按'发送',过程应该重新开始。线程应该创建在同一个对象't'中。应该释放线程对象吗? C#
您是否真的想根据用户输入中止上次发送?不知道发送什么。图像很难说,但这看起来不正确。背景工作者可能是更好的选择。中止应该留给特殊情况,这与杀死一个过程没有什么不同,只有在没有其他选择的情况下才能完成,你应该有很多。 – 2012-04-04 08:58:52
考虑使用更抽象的usign线程方式,至少使用线程池或更好的TPL。关于使用waitHandle的 – 2012-04-04 09:00:38