这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来。
这里主要讲解下CancellationTokenSource,CancellationTokenSource是用于取消线程,具体使用起来有点另类:首先定义实体,然后将其下的属性ToKen传递给线程,当需要取消线程时,调用下Cancel()方法。例子我依然采用了MSDN的例子,但我做了一些修改,这个例子虽然看起来挺复杂,但还是记录了许多内容。
由于不好理解,我就粗略讲解下:
Task
再介绍下factory.ContinueWhenAll,他包含两个参数Task[] tasks,Action
方法执行
英文解释:
CancellationTokenSource source = == =
values[ctr </span>- <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span>] =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">NO Cancel at task {0}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,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;">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> values; },token)); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { Task</span><<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">double</span>> fTask =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;"> { 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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">long</span> sum = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> n = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">foreach</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">var</span> t <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;">foreach</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">var</span> r <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;"> r; n</span>++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> sum / (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">double</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">The mean is {0}.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,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;">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> (AggregateException ae) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">foreach</span> (Exception e <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">in</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> ae.InnerExceptions) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (e <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">is</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,((TaskCanceledException)e).Message); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Exception: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span> +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> 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#线程安全使用(四)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)