Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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(); 
                     //I'd like to tell this imageWrapper instance to cancel the convert process if the user requests so... But how? 
                 }                      
             }
             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
Posted
Updated 5-Oct-14 6:32am
v2
Comments
Sinisa Hajnal 6-Oct-14 2:37am    
Have you tried adding image ID property into ImageWrapper (can be anything, hash, guid, even simple integer) - and canceling by that value?

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