This project is read-only.

How to abord all queued items when an exception is raised..

Jul 5, 2010 at 10:07 PM
Edited Jul 5, 2010 at 10:08 PM

Hello,

I m using smart thread pool and i am dealing with the following problem:

Under certain conditions a background thread must throw a custom exception (lets say OperationCanceledException)

I want to find a way to catch this exception from the caller thread and abort all the  pending/running  items.

Is there a way to do this?

Bellow is a simplified  version of the problem. The whole operation must cancel when DoSomeWork throws an OperationCanceled exception.

Thnx in advance!

 

class Program
    {

        static void Main(string[] args)
        {
            Program p = new Program();

            SmartThreadPool smartThreadPool = new SmartThreadPool();
            for (int i = 0; i < 150; ++i)
            {
                smartThreadPool.QueueWorkItem(new WorkItemCallback(p.DoSomeWork), i);
            }

            smartThreadPool.WaitForIdle();
            smartThreadPool.Shutdown();

        }

        private object DoSomeWork(object i)
        {
            if ((int)i == 100) //A certain condition is met where the whole operation should terminate...
            {
                throw new OperationCanceledException(@"A SEVERE ERROR OCCURED.. ALL OPERATIONS MUST STOP AND CALLER MUST BE NOTIFIED IMMEDIATELY");
            }
            else
            {
                Thread.Sleep(500);
                //Some work is done here.
                return i;
            }
        }

}

Jul 5, 2010 at 11:44 PM

I m posting the solution that i found.

Its not the most elegant, but at least it works as expected and i ll be able to deliver it on time..

 

 class Program
    {
      
        private bool ThreadCancelIndicator = false; //When set to true all background work is skipped.
       
        static void Main(string[] args)
        {
            Program p = new Program();

            SmartThreadPool smartThreadPool = new SmartThreadPool();
            IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
            IWorkItemResult[] wirs = new IWorkItemResult[150];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] =
                    workItemsGroup.QueueWorkItem(new WorkItemCallback(p.DoSomeWork), i);
            }

            smartThreadPool.WaitForIdle();
            smartThreadPool.Shutdown();

             //Iterate to results and find one that has throw an exception.
            if (wirs.Where(x => x.Exception is OperationCanceledException).Count() > 0)
            {
                Console.WriteLine("OPERATION CANCELED EXCEPTION FOUND");
            }

            Console.ReadLine();

        }

        private object DoSomeWork(object i)
        {
            if (ThreadCancelIndicator)
            {
                //Do nothing.Operation canceled.
                return null;
            }
            else
            {
                if ((int)i == 100)
                {
                    ThreadCancelIndicator = true; //A custom exception is about to be throw..So we update ThreadCancelIndicator to true in order all pending items not do anything from now on ,till the queue empties..
                    throw new OperationCanceledException();
                }
                else
                {
                    //Do work.
                    Thread.Sleep(500);
                    Console.WriteLine(i);
                    return i;
                }
            }

           
        }

}