C#线程安全使用(四)

C#线程安全使用(四),第1张

概述这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来。 这里主要讲解下CancellationTokenSource,C

这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来。

这里主要讲解下CancellationTokenSource,CancellationTokenSource是用于取消线程,具体使用起来有点另类:首先定义实体,然后将其下的属性ToKen传递给线程,当需要取消线程时,调用下Cancel()方法。例子我依然采用了MSDN的例子,但我做了一些修改,这个例子虽然看起来挺复杂,但还是记录了许多内容。

由于不好理解,我就粗略讲解下:

Task fTask = factory.ContinueWhenAll(tasks.ToArray(),上面是创建任务,创建10个线程,并且线程中增加了判断,如果随即数等于0就取消该线程。

 再介绍下factory.ContinueWhenAll,他包含两个参数Task[] tasks,Action continuationAction。MSDN的解释是:

 方法执行 

英文解释:

CancellationTokenSource source = == = []>> tasks = List[]>>= ( taskCtr = ; taskCtr <= ; taskCtr++ iteration = taskCtr + =>[] values = [ ( ctr = ; ctr <= ; ctr++= rnd.Next(, (value == values[ctr </span>- <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;1</span>] =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; value; } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;NO Cancel at task {0}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,i<a href="https://www.jb51.cc/tag/tera/" target="_blank" >tera</a>tion); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; values; },token)); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; { Task</span><<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;double</span>> fTask =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; factory.ContinueWhenAll(tasks.ToArray(),(res<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>ts) </span>=><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Cal<a href="https://www.jb51.cc/tag/cula/" target="_blank" >cula</a>ting overall mean...</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;long</span> sum = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span> n = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;foreach</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;var</span> t <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; res<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>ts) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;foreach</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;var</span> r <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; t.Res<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>t) { sum </span>+=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; r; n</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;; } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> sum / (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;double</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)n; },token); Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;The mean is {0}.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,fTask.Res<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>t); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; (AggregateException ae) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;foreach</span> (Exception e <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; ae.InnerExceptions) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (e <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;is</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; TaskCanceledException) Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Unable to co<a href="https://www.jb51.cc/tag/mpu/" target="_blank" >mpu</a>te mean: {0}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,((TaskCanceledException)e).Message); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Exception: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span> +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; e.GetType().<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>); } } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Read<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne();}

}

显示结果图片,每次的结果都不一样的,所以我也是运行了好几次,看这个结果会发现一件事,线程只执行了两个,即当线程2中调用Cancel后,其他线程也被取消了。

总结

以上是内存溢出为你收集整理的C#线程安全使用(四)全部内容,希望文章能够帮你解决C#线程安全使用(四)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264264.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存