现实生活中,我们往往会遇到,要执行一个线程的方法,假如这个方法特别耗时,我怎么才能在指定的线程超时时间内,取消执行,并把线程销毁!以下是本人总结的常见几种方式!特此做下笔记!

1.使用CancellationTokenSource之基于Task实现方式

CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(TimeSpan.FromMilliseconds(2000));
Task task=Task.Factory.StartNew(() =>
{
while (true)
{
try
{
source.Token.ThrowIfCancellationRequested();
Console.WriteLine("子方法执行中..."); Thread.Sleep(1000);
Console.WriteLine("子方法执行完毕...");
}
catch (OperationCanceledException ex)
{
Console.WriteLine("已捕获取消异常:" + ex.Message);
break;
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
break;
} }
Console.WriteLine("线程已终止"); }, source.Token);

2.使用CancellationTokenSource之基于Thread实现方式

 CancellationTokenSource source = new CancellationTokenSource();
Thread thread = new Thread(new ThreadStart(() =>
{ while (true)
{
Console.WriteLine("现在时间:"+DateTime.Now);
}
}));
thread.IsBackground = true;
thread.Start(); source.Token.Register(() =>
{
Console.WriteLine("超时时间已到,开始终止线程");
thread.Abort();
Console.WriteLine("超时时间已到,终止线程完成");
});
source.CancelAfter(2000);

3.基于Thread.Join()实现

 Thread thread =new Thread(new ThreadStart(() =>
{
while (true)
{
Console.WriteLine("现在时间:"+DateTime.Now);
}
}));
thread.Start();
thread.Join(2000);
Console.WriteLine("join超时时间已到,开始终止线程");
thread.Abort();
Console.WriteLine("join超时时间已到,终止线程完成");

4.基于System.Timers.Timer的实现方式

DateTime dtNow = DateTime.Now;
Thread thread = new Thread(new ThreadStart(() =>
{
while (true)
{
Console.WriteLine("现在时间:" + DateTime.Now);
} }));
thread.IsBackground = true;
thread.Start();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += (sender,e) =>
{
double db=DateTime.Now.Subtract(dtNow).TotalSeconds;
if (db > 2)//大于2秒说明已经超时
{
Console.WriteLine("任务已执行超过了:" + db + "秒");
Console.WriteLine("超时时间已到,开始终止线程");
thread.Abort();
Console.WriteLine("超时时间已到,终止线程完成");
timer.Stop();
}
};
timer.Start();

5.基于信号量和CancellationTokenSource的实现方式

AutoResetEvent autoReset = new AutoResetEvent(false);
CancellationTokenSource source = new CancellationTokenSource(); Task.Factory.StartNew(() =>
{
try
{
while (true)
{
source.Token.ThrowIfCancellationRequested();
Console.WriteLine("子线程:Work starting."); // Simulate time spent working.
Thread.Sleep(5000);//new Random().Next(500, 2000)
autoReset.Set();
// Signal that work is finished.
Console.WriteLine("子线程:Work ending."); }
}
catch (Exception e)
{
autoReset.Set();
Console.WriteLine("子线程收到异常:"+e.Message+",线程退出!");
// throw;
} }, source.Token);
Console.WriteLine("主线程只等待1秒");
autoReset.WaitOne(1000);//等待超过1秒,则需要取消该线程
Console.WriteLine("主线程等待超时,开始继续执行,同时调用Source.Cancel()");
source.Cancel();

最新文章

  1. Rsync
  2. Nginx反向代理模板
  3. 系统UINavigationController使用相关参考
  4. [美]莫提默 J. 艾德勒《如何阅读一本书》
  5. DOS命令之----Netstat+Task以及相关使用
  6. Intent过滤,intent-filter
  7. Scipy教程 - 优化和拟合库scipy.optimize
  8. Sqlserver事务隔离级别详解
  9. 启动Eclipse发生错误:An internal error occurred during: "Initializing Java Tooling".
  10. C++日志系统log4cxx使用总结
  11. FLV 格式导入 视频
  12. [Hive_add_10] Hive 的 serde (序列化 & 反序列化) 操作
  13. python 字典的定义以及方法
  14. bzoj 2091 The Minima Game - 动态规划 - 博弈论
  15. Jmeter遇到线程链接被重置(Connection reset by peer: socket write error)的解决方法
  16. MFC+WinPcap编写一个嗅探器之三(WinPcap)
  17. python爬搜狗微信获取指定微信公众号的文章
  18. CUBA-Platform将全面助力中国开发者
  19. August 19th 2017 Week 33rd Saturday
  20. LOJ2503 NOIP2014 解方程 【HASH】

热门文章

  1. Java项目中的一些注意事项
  2. 查询表达式和LINQ to Objects
  3. object detection技术演进:RCNN、Fast RCNN、Faster RCNN
  4. linux下python+pycharm安装
  5. java文件创建和删除
  6. OC 中 @synthesize 关键字介绍和使用
  7. 像写C#一样编写java代码
  8. Java学习笔记——设计模式之一.简单工厂
  9. Mongodb密码安全设置
  10. 【转】典型的JavaScript面试题