关于Thread.Abort
static void Main(string[] args)
{
Thread th = new Thread(Fun);
th.Start();
Thread.Sleep(1000);
th.Abort();
}
static void Fun()
{
try
{
while (true)
{
try
{
Test();
}
catch (ThreadAbortException ex) //在此处ThreadAbortException能被捕获到,但是还是会向上层冒泡
{
Console.WriteLine("线程被终止了");
//由于是在异常捕获中休眠,所以即使其它线程调用此线程的Abort方法结束此线程,也得等休眠时间过去才能将ThreadAbortException异常冒泡到此线程的最顶层代码
Thread.Sleep(3000);
}
}
}
catch (ThreadAbortException ex)//在此处ThreadAbortException能被捕获到,此线程结束
{
Console.WriteLine("结束了");
}
}
static void Test()
{
try
{
Thread.Sleep(100000);
}
catch (ThreadAbortException ex)//在此处ThreadAbortException能被捕获到,但是还是会向上层冒泡
{
Console.WriteLine("Test_ThreadAbortException");
}
catch (Exception ex)
{
Console.WriteLine("Test_Exception");
}
}
{
Thread th = new Thread(Fun);
th.Start();
Thread.Sleep(1000);
th.Abort();
}
static void Fun()
{
try
{
while (true)
{
try
{
Test();
}
catch (ThreadAbortException ex) //在此处ThreadAbortException能被捕获到,但是还是会向上层冒泡
{
Console.WriteLine("线程被终止了");
//由于是在异常捕获中休眠,所以即使其它线程调用此线程的Abort方法结束此线程,也得等休眠时间过去才能将ThreadAbortException异常冒泡到此线程的最顶层代码
Thread.Sleep(3000);
}
}
}
catch (ThreadAbortException ex)//在此处ThreadAbortException能被捕获到,此线程结束
{
Console.WriteLine("结束了");
}
}
static void Test()
{
try
{
Thread.Sleep(100000);
}
catch (ThreadAbortException ex)//在此处ThreadAbortException能被捕获到,但是还是会向上层冒泡
{
Console.WriteLine("Test_ThreadAbortException");
}
catch (Exception ex)
{
Console.WriteLine("Test_Exception");
}
}