首页 > 编程语言 > 详细

[.net 多线程]ThreadPool

时间:2018-05-23 01:31:07      阅读:228      评论:0      收藏:0      [点我收藏+]
1 CancellationTokenSource tocken = new CancellationTokenSource();
2 ThreadPool.QueueUserWorkItem(param => {
3     Console.WriteLine($"当前线程【{Thread.CurrentThread.ManagedThreadId}】");
4     Thread.Sleep(1000);
5     Console.WriteLine($"当前线程【{Thread.CurrentThread.ManagedThreadId}】退出");
6 }, tocken);

 

两种出发模式:定时8s出发,事件触发器出发(autoresetevent)

 1 AutoResetEvent evt = new AutoResetEvent(false);
 2 ThreadPool.RegisterWaitForSingleObject(evt, (state, isOut) =>
 3 {
 4     CancellationTokenSource token = (CancellationTokenSource)state;
 5     if (tocken.IsCancellationRequested)
 6     {
 7         Console.WriteLine("退出RWFS");
 8         return;
 9     }
10     Console.WriteLine("RWFS...");
11 }, tocken, 8000, false);

 

[.net 多线程]ThreadPool

原文:https://www.cnblogs.com/deepminer/p/9074606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!