We use .Net 3.5 and C# SmartThreadPool by Ami Bar to manage multiple work items. When our app shuts down we would like to “tell” all work items that they should try to complete their work as soon as possible, then give them a grace period to complete and then kill the ones still running. Our work items are cooperative and they need to be able to check if they were “told” to complete work and get out of long-running loops.
It sounded like a good idea to use workItemResult.Cancel(false) to “tell” work items to complete and then use SmartThreadPool.IsWorkItemCanceled inside of work items to stop their work, but it turns out that WaitAll does not wait on cancelled items and hence we can’t block for the grace period.
I started playing with creating a WorkItemGroup for each work item, call Cancel(false) on work item and then workItemGroup.WorkForIdle(gracePeriod) but wanted to check if this is worth pursuing.
I’d appreciate any thoughts on this.
Thanks
I ended up patching SmartThreadPool as described here http://smartthreadpool.codeplex.com/discussions/345937