也许我在想这个但是我有很多线程正在做一个相当漫长的过程,我希望能够在必要时干净地中止它们。这主要是因为我不想在旧线程完成之前启动新线程。有没有办法告诉通过事件或其他方法线程完全中止?或者我不应该担心这个?目前我的中止方法代码如下:
private void AbortProcessing()
{
if (!Processing) return;
StopFlag = true;
for (int x = 0; x < MaxThreads; x++)
{
try
{
BW[x].CancelAsync();
}
catch { }
//Processing = false;
}
bool aborted = false;
while (!aborted)
{
aborted = true;
for (int x = 0; x < MaxThreads; x++)
{
if (BW[x].IsBusy) aborted = false;
}
Thread.Sleep(100);
}
}
在信号中止之后,IsBusy永远是真的,所以这不起作用。有什么建议吗?
答案 0 :(得分:0)
试试这个:这个简单的样本
Thread o;
private void button1_Click(object sender, EventArgs e)
{
ThreadStart starter = ThreaadFunction;
starter += () => {
//this is this event you want to do any thing after a threaad finished the job
};
o = new Thread(starter) {IsBackground =true };
o.Start();
}
public void ThreaadFunction()
{
Thread.Sleep(1000);
// what you do in thread
}
}