线程设置超时
var cts = new CancellationTokenSource(); Thread t = new Thread(new ThreadStart(() => { Console.WriteLine(string.Format("{1}线程Id:{0}开始运行", Thread.CurrentThread.ManagedThreadId, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); Thread.Sleep(5 * 1000); Console.WriteLine(string.Format("{1}线程Id:{0}运行完成", Thread.CurrentThread.ManagedThreadId, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); })); cts.Token.Register(() => { Console.WriteLine(string.Format("{1}线程Id:{0}运行被中止", t.ManagedThreadId, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); t.Abort(); }); cts.CancelAfter(1 * 1000); t.Start(); t.Join(); Console.WriteLine(string.Format("{1}线程Id:{0}结束", t.ManagedThreadId, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));