C#中的并行编程 [英] Parallel programming in C#

查看:116
本文介绍了C#中的并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对学习C#.NET中的并行编程感兴趣(不像所了解的一切,而是基础知识和一些好的做法),因此,我决定对我的旧程序进行重新编程.称为ImageSyncer. ImageSyncer是一个非常简单的程序,它的作用是扫描文件夹并查找所有以.jpg结尾的文件,然后根据文件的拍摄日期(解析xif数据或任何其他内容)计算文件的新位置.它被称为).生成位置后,程序将检查该位置上是否存在任何现有文件,如果存在,则会查看要复制的文件和以其方式"文件的最后写入时间.如果这些相等,则文件将被跳过.如果不是,则创建并匹配两个文件的md5校验和.如果没有匹配项,将为要复制的文件提供一个要复制到的新位置(例如,如果要复制到"C:\ test.jpg",则将其复制到"C:\ test(1)). jpg").该操作的结果被填充到一个struct-type队列中,该队列包含两个字符串,即原始文件和复制位置.然后对该队列进行迭代,直到队列为空并复制文件.

I'm interested in learning about parallel programming in C#.NET (not like everything there is to know, but the basics and maybe some good-practices), therefore I've decided to reprogram an old program of mine which is called ImageSyncer. ImageSyncer is a really simple program, all it does is to scan trough a folder and find all files ending with .jpg, then it calculates the new position of the files based on the date they were taken (parsing of xif-data, or whatever it's called). After a location has been generated the program checks for any existing files at that location, and if one exist it looks at the last write-time of both the file to copy, and the file "in its way". If those are equal the file is skipped. If not a md5 checksum of both files is created and matched. If there is no match the file to be copied is given a new location to be copied to (for instance, if it was to be copied to "C:\test.jpg" it's copied to "C:\test(1).jpg" instead). The result of this operation is populated into a queue of a struct-type that contains two strings, the original file and the position to copy it to. Then that queue is iterated over untill it is empty and the files are copied.

换句话说,有4种操作:

In other words there are 4 operations:

1. Scan directory for jpegs  
2. Parse files for xif and generate copy-location  
3. Check for file existence and if needed generate new path  
4. Copy files

因此,我想重写该程序以使其与parallell并能够同时执行多个操作,而我想知道实现该目标的最佳方法是什么.我想出了两个我可以想到的不同模型,但是它们两个都不是很好.第一个是并行化旧程序的4个步骤,以便在执行步骤1时,它在多个线程上完成,并且在完成步骤1的整个步骤时,开始执行步骤2.另一个(我发现更有趣,因为我不知道该怎么做)是创建一种工作人员和使用者模型,因此当线程在步骤1中完成时,另一个将接管并执行步骤2.对象(或类似的东西).但是正如我所说,我不知道这些解决方案是否很好.另外,我对并行编程一无所知.我知道如何制作线程,以及如何使其以对象作为唯一参数来执行功能,而且我也曾经使用过BackgroundWorker类,但是我对它们都不熟悉

And so I want to rewrite this program to make it paralell and be able to perform several of the operations at the same time, and I was wondering what the best way to achieve that would be. I've came up with two different models I can think of, but neither one of them might be any good at all. The first one is to parallelize the 4 steps of the old program, so that when step one is to be executed it's done on several threads, and when the entire of step 1 is finished step 2 is began. The other one (which I find more interesting because I have no idea of how to do that) is to create a sort of worker and consumer model, so when a thread is finished with step 1 another one takes over and performs step 2 at that object (or something like that). But as said, I don't know if any of these are any good solutions. Also, I don't know much about parallel programming at all. I know how to make a thread, and how to make it perform a function taking in an object as its only parameter, and I've also used the BackgroundWorker-class on one occasion, but I'm not that familiar with any of them.

任何输入将不胜感激.

推荐答案

选项很少:

任务并行库(TPL):如果您对基本的线程原语和概念感兴趣: C#中的线程

If you are interested in basic threading primitives and concepts: Threading in C#

[但是正如@John Knoeller指出的那样,您给出的示例很可能是顺序I/O绑定的.]

[But as @John Knoeller pointed out, the example you gave is likely to be sequential I/O bound]

这篇关于C#中的并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆