Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
foreach (Item item in findResults)
                      {
                          ThreadPool.QueueUserWorkItem(delegate
                          {
                              StartProcess(item);
                          }, null);
                          Thread.Sleep(1000);
                      }
Posted
Updated 24-Nov-16 1:43am

1 solution

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.
 
Share this answer
 
Comments
User1454 24-Nov-16 22:20pm    
Nice explaination. Thanks. I will try for sure.
User1454 27-Nov-16 23:51pm    
Hi John Simmons / outlaw,
Thanks a lot for the solution. I tried TPL and its pretty fast. But I want to know is that some thing like if there are 1000 files, 1000 threads are created or it occupies the thread which is free or not alive ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900