如何在C#中同时将多个文件保存到磁盘 [英] How to save multiple files to the disk at the same time in C#

查看:238
本文介绍了如何在C#中同时将多个文件保存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要创建一个简单的应用程序,将文件并行保存到磁盘。 For.eg如果有10封邮件,则应读取每封邮件并将附件并行保存到磁盘。我怎样才能使用多线程?

1stMail - 执行任务

2ndMail - 执行任务。

|

|

10thMAil-执行任务。

同样所有这些都应该平行完成,而不是一个接一个地完成。



我尝试过:



Hello everyone,
I need to create a simple application that should save the files to disk parallely. For.eg If there are 10 mails, each mail should be read and save the attachments to the disk parallely. How can I do that using multithreading?
1stMail - Performing the task
2ndMail - Performing the task.
|
|
10thMAil- Performing the task.
Similarly all these should be done parallely instead of one after the other.

What I have tried:

foreach (Item item in findResults)
                      {
                          ThreadPool.QueueUserWorkItem(delegate
                          {
                              StartProcess(item);
                          }, null);
                          Thread.Sleep(1000);
                      }

推荐答案

好吧,我建议您使用任务并行库而不是您正在使用的方式。



另一方面,当你接下来的时候,你不能*技术上*同时写多个文件,因为硬盘磁头只能一次只能在一个地方。当然,这个规则的例外是如果你写的是多个硬盘驱动器,但我很确定这不是你正在做的事情。



自从我没有任何代码可以做你想要的,我用Google搜索了下面的短语 - c#使用TPL写多个文件 - 并提出了超过80万个可能的结果。为了不剽窃别人的解决方案,我留给你找到哪一个最适合你的申请。
Well, I would suggest that you use the Task Parallel Library instead of the way you're doing it.

On the other hand, when you get right down to it, you can't *technically* write multiple files at the same time because the hard disk heads can only be in one spot at a time. Of course, the exception to this rule would be if you were writing to multiple hard drives, but I'm pretty sure that's not what you're doing.

Since I don't have any code that does what you want, I googled the following phrase - "c# use TPL to write multiple files" - and came up with over 800 thousand possible results. In order not to plagiarize someone else's solution, I leave it to you to find which one is most appropriate for your application.


这篇关于如何在C#中同时将多个文件保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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