CancellationTokenSource tocken = new CancellationTokenSource();
ThreadPool.QueueUserWorkItem(param => {
Console.WriteLine($"当前线程【{Thread.CurrentThread.ManagedThreadId}】");
Thread.Sleep();
Console.WriteLine($"当前线程【{Thread.CurrentThread.ManagedThreadId}】退出");
}, tocken);
两种出发模式:定时8s出发,事件触发器出发(autoresetevent)
AutoResetEvent evt = new AutoResetEvent(false);
ThreadPool.RegisterWaitForSingleObject(evt, (state, isOut) =>
{
CancellationTokenSource token = (CancellationTokenSource)state;
if (tocken.IsCancellationRequested)
{
Console.WriteLine("退出RWFS");
return;
}
Console.WriteLine("RWFS...");
}, tocken, , false);