惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - 残香恨

WinForm:如何设置DataGridView列标题对齐方式 WinForm:如何在ListBox中添加CheckBox ASP.NET WebForm开发WAP网站 lock语句的递归问题 VS 2010 调试 .NET Framework 源代码 最近遇到的两个问题 ASP.NET MVC 2 模板化辅助方法 SynchronizationContext对Windows Forms窗体控件的更新方法 - 残香恨 SQL Server 2008 Express 升级R2全过程 自定义ASP.NET MVC Html辅助方法 - 残香恨 将ASP.NET MVC 1.0升级到ASP.NET MVC 2的三种方法 .NET 4.0:一段动态绑定代码的底层初级分析 Visual Studio 2010 RTM版安装初体验 .NET中的线程 .NET 4.0 任务(Task) C#4.0 动态绑定(Dynamic Binding) .NET4.0新功能:任务(Task) Visual Studio 2010 RC初体验 SQL Server导入文本文件时选择相同数据类型的一个小技巧
.NET4.0线程池的Cooperative Cancellation模式
残香恨 · 2010-02-27 · via 博客园 - 残香恨

刚装了VS2010RC版,体验一下.NET4.0线程池新增的Cooperative Cancellation模式。

  用过.NET线程池的人都知道,一旦把要执行的代码交给线程池去执行后,我们基本上就失去了代码在运行中的控制能力。比如,我们想在某个时刻取消这段代码的执行,就得另想一个办法。随着.NET4.0到来,这个问题得到了解决。.NET4.0引入了一种新的设计模式---合作取消模式(Cooperative Cancellation)。

  .NET4.0的System.Threading命名空间下新增了两个成员,一个是CancellationTokenSource类,一个是CancellationToken结构。CancellationTokenSource类的主要成员如下:

代码

public sealed class CancellationTokenSource : IDisposable {
public CancellationTokenSource();
public void Dispose();
public Boolean IsCancellationRequested { get; }
public CancellationToken Token { get; }//CancellationToken结构
public void Cancel();
public void Cancel(Boolean throwOnFirstException);
}

  CancellationToken结构代码大致如下:

代码

 public struct CancellationToken
    { 
        
public Boolean IsCancellationRequested { get; }
        
public void ThrowIfCancellationRequested();
        
// 当CancellationTokenSource取消时使用的信号量
        public WaitHandle WaitHandle { get; }
        
public static CancellationToken None { get; }
        
public Boolean CanBeCanceled { get; }
        
public CancellationTokenRegistration Register(Action<Object> callback, Object state, Boolean useSynchronizationContext);
    }

  使用代码如下:

代码

using System;
using System.Threading;namespace CooperativeCancellation
{
    
class Program
    {
        
static void Main(string[] args)
        {
            CancellationTokenSource cts 
= new CancellationTokenSource();
            ThreadPool.QueueUserWorkItem(o 
=> ExecuteInThreadPool(cts.Token));
            Console.WriteLine(
"Press <Enter> to cancel the operation.");
            Console.ReadLine();
            cts.Cancel();
            Console.ReadLine();
        }
static void ExecuteInThreadPool(CancellationToken token)
        {
            Console.WriteLine(
"Enter in ThreadPool.");
            
while (!token.IsCancellationRequested)
            {
                Console.Write(
" .");
                Thread.Sleep(
1000);
            }
            Console.WriteLine(
"Operation was be cancel.");
        }
    }
}

  代码简单,我在这里就不多说了。总之,有了Cooperative Cancellation模式后,我们对.NET线程池中的代码的执行有了更大的控制力度,真是个不错的功能。