-3

i want to create users based on the number of photos in a folder.

for example:

user.1 random(4)[photos1-4]
dosomething(user.1)

user.2 random(6)[photos5-10]
dosomething(user.2)

user.3 random(3)[photos11-13]
dosomething(user.3)

user.last [photos.leftover]
dosomething(user.last)

ideas on how to do this?

CurlyFro
  • 1,862
  • 4
  • 22
  • 39

1 Answers1

0

The best way to do this is load your list of work, randomize the list, then put the list in to a queue of some form to be pulled out by the end workers.

private BlockingCollection<string> GetWorkSoruce()
{
    List<string> sourceList = GetListOfFiles(); //How you get your list is up to you.

    Shuffle(sourceList); //see http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp

    //Create a thread safe queue that many consumers can pull from.
    var collection = BlockingCollection<string>(new ConcurrentQueue<string>(sourceList));
    collection.CompleteAdding();

    return collection;
}

Now each of your workers (Users) can pull out of the queue and be given work to do. Because the queue is a ConcurrentQueue you can have many workers from many threads all working at the same time.

private void WorkerDoWork(BlockingCollection<string> workSource, int itemsToTake)
{
    foreach(var imagePath in workSource.GetConsumingEnumerable().Take(itemsToTake))
    {
        ProcessImage(imagePath);
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431