Hello,
I'm trying to implement some sort of image processing management system. Basically the idea is that a user sequentially chooses images, and these images (type: ImageWrapper) are added to a queue. As soon as an image is dequeued, some sort of image conversion is started (e.g. imageWrapper.convert()).
So far I have this code:
private readonly BlockingCollection<ImageWrapper> _imageList = new BlockingCollection<ImageWrapper>();
const int max_task_count = 5;
private Task[] _taskList;
...
private void startProcessing()
{
for (var i = 0; i < max_task_count; i++)
{
var iProcessingTask = Task.Factory.StartNew(() =>
{
try
{
foreach (var item in _imageList.GetConsumingEnumerable(TokenSource.Token))
{
if (TokenSource.IsCancellationRequested)
{
return;
}
ImageWrapper imageWrapper = (ImageWrapper)item;
imageWrapper.convert();
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation cancelled");
}
});
_taskList[i] = iProcessingTask;
}
}
This actually works great so far. But if this was the sequence of actions:
1) The user generates 10 image processing jobs one after another
2) The system starts converting the first of the generated tasks
3) The user wants to abort just one single image process (e.g. #3) without stopping the conversion of other image processes.
Somehow I need to fetch the right task from the taskList (where the right ImageWrapper currently executes the conversion), and tell the imageWrapper to stop the conversion and continue with the next imageWrapper.
Could anyone please give me a hint on this?
Thanks a lot