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.