private static void TimeoutTest1()
{
var cts = new CancellationTokenSource();
var thread = new Thread(() =>
{
Console.WriteLine(String.Format("線程{0}執(zhí)行中", Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(10000);
Console.WriteLine(String.Format("線程{0}執(zhí)行中", Thread.CurrentThread.ManagedThreadId));
});
cts.Token.Register(() =>
{
thread.Abort();
});
cts.CancelAfter(1000);
thread.Start();
thread.Join();
Console.WriteLine(String.Format("線程{0}的狀態(tài):{1}", thread.ManagedThreadId, thread.ThreadState));
}
private static void TimeoutTest2()
{
var thread = new Thread(() =>
{
Console.WriteLine(String.Format("線程{0}執(zhí)行中", Thread.CurrentThread.ManagedThreadId));
Thread.Sleep(10000);
Console.WriteLine(String.Format("線程{0}執(zhí)行中", Thread.CurrentThread.ManagedThreadId));
});
thread.Start();
thread.Join(1000);
thread.Abort();
Console.WriteLine(String.Format("線程{0}的狀態(tài):{1}", thread.ManagedThreadId, thread.ThreadState));
}