WorkItemGroup creates child workitemgroup

Apr 25, 2009 at 9:44 PM
We are in need of WorkItemGroup to create child Workitemgroups or how smartthreadpool help us to resolve our below requirement

1) We do have Sequence of actions - called as task
2) Our aim is to get Response i.e. execute multiple tasks concurrently. Here your worktimegroup helps us to concurrenly run each tasks.
3) However each task sometimes might have few subtasks which again can run concurrently however how to achieve that is not clear with this. Those subtasks again can have few sub-subtasks which can run concurrently again.

Any suggestions?


Apr 26, 2009 at 6:13 PM
Edited Apr 26, 2009 at 6:13 PM
Hi Swap,

Below is a code snippet of how to do what you want.
I assumed you want the response to be asynchronous so I used a call back for the completion.
If you want to wait for the task to complete then use task.WaitForIdle().
The code is only an example. The response can be another class (i.e. Dictionary<K,T>).
The number of argument can be different, etc.

    class Response
        public object Result { get; set; }

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

        private void DoWork()
            // Create a Smart Thread Pool
            SmartThreadPool stp = new SmartThreadPool();

            // Queue a task
            // stp - The SmartThreadPool
            // OnTaskCompleted - The method to call on completion of the task
            // MainTask - The main method of the task, which splits into subtasks
            // 5 - An argument to pass to the MainTask mathod
            QueueTask(stp, OnTaskCompleted, MainTask, 5);

        private void QueueTask(
            SmartThreadPool stp,
            Action<Response> taskCompleteCallback,
            System.Action<IWorkItemsGroup, Response, object> action,
            object state)
            // Create a response object
            Response response = new Response();

            // Create a WorkItemsGroup for the task
            IWorkItemsGroup task = stp.CreateWorkItemsGroup(1);

            // Tell the WorkItemsGroup to call to taskCompleteCallback when its queue is empty.
            task.OnIdle += (wig => taskCompleteCallback(response));

            // Queue the main method of the task
            task.QueueWorkItem(() => action(task, response, state));

        private void MainTask(IWorkItemsGroup task, Response response, object state)
            // Split state into substates
            object [] subStates = new object[5];

            // Queue each sub task into the WorkItemsGroup
            for (int i = 0; i < subStates.Length; i++)
                int index = i;
                task.QueueWorkItem(() => SubTask(task, response, subStates[index]));

        private void SubTask(IWorkItemsGroup task, Response response, object state)
            // Split to subtasks as in MainTask or set the response's result

            response.Result = 42;

        private void OnTaskCompleted(Response response)
            // This method is called after all queue method completed, hence when your task complete.

            // Do something with response.