static void Parallel2()
{
ParallelLoopResult result = Parallel.For(, , i =>
{
Log($"S:{i} ");
Task.Delay().Wait();
Log($"E:{i} ");
});
WriteLine($"IsCompleted:{result.IsCompleted}");
} static void Parallel1WithAsync() {
ParallelLoopResult result = Parallel.For(, , async i =>
{
Log($"S:{i} ");
await Task.Delay();
Log($"E:{i} ");
});
WriteLine($"IsCompleted:{result.IsCompleted}");
} static void Log(string prefix) {
WriteLine($"{prefix}, taks:{Task.CurrentId}, thread:{Thread.CurrentThread.ManagedThreadId}");
}

Task.Delay(10).Wait(); Result

S:1 , taks:3, thread:1
S:3 , taks:4, thread:4
S:5 , taks:6, thread:5
S:7 , taks:9, thread:6
E:7 , taks:9, thread:6
S:8 , taks:13, thread:6
E:3 , taks:4, thread:4
S:4 , taks:15, thread:4
E:1 , taks:3, thread:1
S:2 , taks:3, thread:1
E:5 , taks:6, thread:5
S:6 , taks:11, thread:8
E:2 , taks:3, thread:1
E:8 , taks:13, thread:6
E:4 , taks:15, thread:4
E:6 , taks:11, thread:8
IsCompleted:True

await Task.Delay(10)result:

S:1 , taks:3, thread:1
S:5 , taks:5, thread:5
S:6 , taks:5, thread:5
S:8 , taks:5, thread:5
S:4 , taks:5, thread:5
S:3 , taks:4, thread:3
S:7 , taks:7, thread:6
S:2 , taks:3, thread:1
IsCompleted:True
E:3 , taks:, thread:6
E:6 , taks:, thread:6
E:5 , taks:, thread:6
E:1 , taks:, thread:6
E:4 , taks:, thread:5
E:2 , taks:, thread:6
E:7 , taks:, thread:5
E:8 , taks:, thread:3

Parallel.For with await and wait()的更多相关文章

  1. 令人清爽的异步函数async、await

    1.什么是async.await? async用于声明一个函数是异步的.而await从字面意思上是"等待"的意思,就是用于等待异步完成.并且await只能在async函数中使用; ...

  2. C#异步编程

    什么是异步编程 什么是异步编程呢?举个简单的例子: using System.Net.Http; using System.Threading.Tasks; using static System.C ...

  3. .NET进阶篇06-async异步、thread多线程2

    知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂 内容目录 一.线程Thread1.生命周期2.后台线程3.静态方法1.线程本地存储2.内存栅栏4.返回值二.线程池ThreadPool1.工作队 ...

  4. Thread&ThreadPool、Parallel、Async和Await用法总结

    1.线程和线程池Thread&ThreadPool //线程初始化时执行方法可以带一个object参数,为了传入自定义参数,所以执行需单独调用用于传参. Console.WriteLine(& ...

  5. Thread、ThreadPool、Task、Parallel、Async和Await基本用法、区别以及弊端

    多线程的操作在程序中也是比较常见的,比如开启一个线程执行一些比较耗时的操作(IO操作),而主线程继续执行当前操作,不会造成主线程阻塞.线程又分为前台线程和后台线程,区别是:整个程序必须要运行完前台线程 ...

  6. await and async

    Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...

  7. 异步方法的意义何在,Async和await以及Task的爱恨情仇,还有多线程那一家子。

    前两天刚感受了下泛型接口的in和out,昨天就开始感受神奇的异步方法Async/await,当然顺路也看了眼多线程那几个.其实多线程异步相关的类单个用法和理解都不算困难,但是异步方法Async/awa ...

  8. (译)关于async与await的FAQ

    传送门:异步编程系列目录…… 环境:VS2012(尽管System.Threading.Tasks在.net4.0就引入,在.net4.5中为其增加了更丰富的API及性能提升,另外关键字”async” ...

  9. Async and Await

    http://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...

随机推荐

  1. visual studio code 编辑器的配置及快捷键等, vscode, csc

    visual studio code (vsc) 对开发node.js,javascript,python,html,golang等比较友好,同时支持git浏览及分屏对比,运行速度快,所以是值得一用的 ...

  2. Linux实验楼学习之二

    新建文件1-1.c touch 1-1.c 编辑文件1-1.c gedit 1-1.c 生成可执行文件1-1.c gcc -o 1-1 1-1.c 执行可执行文件1-1 ./1-1

  3. JS 转整型

    1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4, ...

  4. windows 7 中使用命令行创建WiFi热点

    就是让你的电脑可以作为WiFi热点,然后供其它支持WiFi的设备上网 首先你的电脑中必须有正常使用的无线网卡 幺幺幺切克闹,开始命令吧,(注:命令是在windows中的命令提示符中运行的) 禁用承载网 ...

  5. C# Bulk Operations(转)

    转自http://blog.csdn.net/winnyrain/article/details/51240684 Overcome SqlBulkCopy Limitations with C# B ...

  6. mysql在windows下命令行启动与关闭服务

    一.命令行关闭与启动服务的命令: 二.运行cmd的命令行程序时,必须以管理员身份运行.否则会出现如下提示: 三.mysql5.7版本的服务名一般默认为mysql57,因此如果使用服务名mysql,那么 ...

  7. Git—使用方法

    1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...

  8. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  9. HZNU_TI1050 训练实录

    菜鸡队训练实录 比赛记录:[名称:奖项 / 排名] 2018: ZJPSC                       Bronze      / 86 CCPC Jilin              ...

  10. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) C. Maximum splitting

    地址: 题目: C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input ...