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. java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换【不推荐,推荐Oakley】

    概述 信息安全基本概念: DH(Diffie–Hellman key exchange,迪菲-赫尔曼密钥交换) DH 是一种安全协议,,一种确保共享KEY安全穿越不安全网络的方法,它是OAKLEY的一 ...

  2. PAT 1054 The Dominant Color[简单][运行超时的问题]

    1054 The Dominant Color (20)(20 分) Behind the scenes in the computer's memory, color is always talke ...

  3. end=‘’

    print('----------------') a = ['aa','bb','cc','dd','ee'] for i in range(len(a)): print(i,a[i])#默认换行 ...

  4. 使用SQLMAP对网站和数据库进行SQL注入攻击

    from:http://www.blackmoreops.com/2014/05/07/use-sqlmap-sql-injection-hack-website-database/ 0x00 背景介 ...

  5. ASP.NET Post方式提交

    public static string SendMsg(string fxPhone, string fxPassword, string toPhone, string msg) { try { ...

  6. 文件操作(CRT、C++、WIN API、MFC)

    一.使用CRT函数文件操作 二.使用标准C++库 std::fstream std::string 1)std::string对象内部存储了一个C的字符串,以'\0'结尾的. 2)std::strin ...

  7. curl命令总结

    curl常用命令http://www.cnblogs.com/gbyukg/p/3326825.html curl命令后面的网址需要用双引号括起来,原因:防止有特殊字符 &号就是特殊字符 cu ...

  8. .NET 和 .NET框架概览

    什么是.NET .NET是为简化在第三代因特网的分布式环境下的应用程序的开发,基于开放互联网标准和协议之上,实现异质语言和平台高度交互性而构建的新一代计算和通信平台.其主要由三部分构成:.NET框架. ...

  9. iOS 建立项目过滤机制 —— 给工程添加忽略文件.gitignore

        目前iOS 项目 主要忽略 临时文件.配置文件.或者生成文件等,在不同开发端这些文件会大有不同,如果 git add .把这些文件都push到远程, 就会造成不同开发端频繁改动和提交的问题. ...

  10. 怎样把QQ群降级(1000人降到200或500人,500人降到200)

    怎样把QQ群降级(1000人降到200或500人,500人降到200)QQ群只有升级的选项,没有降级选项,一旦升级为1000人,就无法直接降级为200人或500人,建群时选择了500人也无法降到200 ...