views:

59

answers:

3

I have an array that is a list of files in a folder, I'm processing the files and renaming them. It takes about 15 minutes to rename them all, this is done daily. I currently have 1 Backgroundworker to handle this and update the UI.

My question is, this: How can I use more than 1 Backgroundworker to use more than 25% of the CPU to do this?

Split the array into 3 parts and call 3 separate workers to do their part?

+3  A: 

If you're using .NET 4.0 then you can use Parallel Extensions which will effectively do this for you - you'd just call

Parallel.ForEach(files, file =>
{
    // Call file processor here
});

Note that if your task is heavily IO bound, using multiple threads may slow it down instead of speeding it up.

Jon Skeet
I'ts a 3.5 app, but there is no reason it cannot be upgraded to 4.0, I'll look into that, thank you so much for you input.
Landmine
+1  A: 

Hi,

You could create a worker class that accepts a folder path in the constructor. This worker class is responsible for renaming all the files within the context of the folder it is initialized with. The class could have a DoWork() method where all your renaming work would occur. This will allow you to create 1 worker class per folder and then start each worker class on a separate thread thus splitting the work up however you would like. Setting the thread priority higher will give you more of time on the cpu.

System.Threading namespace contains what you need.

public class worker
{
   private string _folderPath = string.Empty;

   public worker(string folderPath)
   {
     _folderPath = folderPath
   }

   public void DoWork()
   {
     //work happens here
   }
}


worker fileWorker = new worker("path to file folder");
Thread newThread = new Thread(fileWorker.DoWork);
newThred.Start();

Enjoy!

Doug
Thanks Doug, however, all of these files are in 1 folder... It's an upload folder that is just filled!
Landmine
A: 

If there is little variability in time to process each item then splitting the array and giving each worker a portion may be ok. However you may find it neater to keep the list centrally and have each worker say "give me some work to do", this should ensure that the work is shared out more evenly. It's also resilient to a more variable population of workers, workers could come and go as system resources permit.

djna