c# tpl线程_任务并行库tpl编写多线程代码

本文介绍了如何利用C#的任务并行库TPL来编写高效的多线程代码,帮助开发者提升程序的并发性能。
摘要由CSDN通过智能技术生成

c# tpl线程

I am a big fan of executing multi-threads in an application, and it is interesting to see how quickly parallelism can solve a complex query.

我是在应用程序中执行多线程的忠实拥护者,很有趣的是,看看并行处理如何快速解决复杂的查询。

让我们通过以下内容了解TPL: (Let’s understand TPL with the following content:)

  • Write a simple “Parallel.For” loop

    编写一个简单的“ Parallel.For”循环

  • Write a simple “Parallel.ForEach” loop

    编写一个简单的“ Parallel.ForEach”循环

  • Cancel “Parallel.For” or “Parallel.ForEach” loop

    取消“ Parallel.For”或“ Parallel.ForEach”循环

  • Exception Handling in parallel loops

    并行循环中的异常处理

“ Parallel.For”循环 (The “Parallel.For” Loop)

The below function takes the directory name from the command line and uses the “TPL For loop” to yield results.

下面的函数从命令行获取目录名称 ,并使用“ TPL For循环”产生结果。

Let’s consider an example of iterating a directory and output the number of files inside the directory, not subfolder files though, along with total files size in bytes.

让我们考虑一个迭代目录的示例,并输出目录中的文件数(而不是子文件夹文件)以及文件的总大小(以字节为单位)。

输出量 (Output)

The below result displays both positive and negative scenarios, i.e., for valid and invalid directory names.

以下结果显示了肯定和否定情况,即有效和无效目录名。

//Valid directory scenario
Enter valid directory path :
E:\gifs//Result
Directory 'E:\gifs':, 7 files, 2,313,294 bytes//Invalid directory Result
//
The specified directory does not exist.

“ Parallel.ForEach”循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值