0

I am looking for a mechanism already in c# that will allow me to do something like this:

  • 10 images need to be decoded
  • there is only enough memory to decode 2
  • start decoding 2, put the rest in a job queue
  • ability to cancel tasks

Any recommendations on using c# accomplish something like this?

Kara
  • 6,115
  • 16
  • 50
  • 57
user1660943
  • 45
  • 1
  • 4

2 Answers2

1

The BlockingCollection<T> class makes producer/consumer queues very easy to work with.

var queue = new BlockingCollection<Action>();

//put work to do in the queue
for (int i = 0; i < 10; i++)
    queue.Add(() => ProcessImage());
queue.CompleteAdding();

//create two workers
for (int i = 0; i < 2; i++)
{
    Task.Factory.StartNew(() =>
    {
        foreach (var action in queue.GetConsumingEnumerable())
            action();
    });
}

//to cancel the work, empty the queue
Task.Delay(5000)
    .ContinueWith(t =>
    {
        queue.GetConsumingEnumerable().Count();
    });
Servy
  • 202,030
  • 26
  • 332
  • 449
0

use Seamphore + ConcurrentQueue combo

TalentTuner
  • 17,262
  • 5
  • 38
  • 63