var response = RunTaskWithTimeout<ReturnType>(
(Func<ReturnType>)delegate { return SomeMethod(someInput); }, ); /// <summary>
/// Generic method to run a task on a background thread with a specific timeout, if the task fails,
/// notifies a user
/// </summary>
/// <typeparam name="T">Return type of function</typeparam>
/// <param name="TaskAction">Function delegate for task to perform</param>
/// <param name="TimeoutSeconds">Time to allow before task times out</param>
/// <returns></returns>
private T RunTaskWithTimeout<T>(Func<T> TaskAction, int TimeoutSeconds)
{
Task<T> backgroundTask; try
{
backgroundTask = Task.Factory.StartNew(TaskAction);
backgroundTask.Wait(new TimeSpan(, , TimeoutSeconds));
}
catch (AggregateException ex)
{
// task failed
var failMessage = ex.Flatten().InnerException.Message);
return default(T);
}
catch (Exception ex)
{
// task failed
var failMessage = ex.Message;
return default(T);
} if (!backgroundTask.IsCompleted)
{
// task timed out
return default(T);
} // task succeeded
return backgroundTask.Result;
}

https://stackoverflow.com/questions/13513650/how-to-set-timeout-for-a-line-of-c-sharp-code

最新文章

  1. 利用XAG在RAC环境下实现GoldenGate自动Failover
  2. E:“图片视频”的列表页(taxonomy-cat_media.php)
  3. LINQ to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”
  4. 如何查看Git对象
  5. 20145330《Java程序设计》课程总结
  6. 9.20 java继承
  7. codeforces 429E
  8. poolboy的坑
  9. C# Web Api 上传文件
  10. HDU 1561-The more, The Better(树状背包)
  11. oracle数据同步方案
  12. 一些有用的webservice
  13. 实现对properties文件的有序读写
  14. 缓存的概念(反向代理、CDN)
  15. Eclipse快捷键 10个最有用的快捷键(转)
  16. 不同浏览器使用Content-disposition时filename带空格的处理方式不同
  17. VS中,NUnit适合测试者尽心开发自动化测试,而Unit适合开发者开发单元测试。
  18. VS2015转VS2008
  19. PHP简单实现购物车
  20. Struts2如何搭建?

热门文章

  1. PhpStorm中如何使用database工具,详细操作方法
  2. Spring整合Hibernate实现Spring Data JPA (简单使用)
  3. nginx部署网站step by step
  4. 2017 趋势科技 研发4.26(offer)
  5. wx小程序知识点(三)
  6. HTML标签与属性
  7. 51 Nod 1085 01背包问题
  8. [转载]blktrace分析IO
  9. confluence 附件docx文件 乱码处理
  10. 10个PHP代码片段