That looks fantastic Greg. Great find. I don't think I would help me as my Parallel for is at a much higher level than my Crypto utils class but I already have ideas where I could use this.
On Sat, Feb 16, 2013 at 8:59 AM, Greg Keogh <[email protected]> wrote: > Chaps, I think I have found the formally correct way of giving each > Parallel ForEach thread its own copy of a disposible and unsharable class. > There is an overload of ForEach that lets you do something at the start and > end of each worker thread. Below is a skeleton of my code. It's interesting > to put trace displays in the 3 callback blocks and look at the lifetime of > the threads and the order in which things happen. I haven't experimented > with cancellation or exception handling yet in the scenario below, but it's > important I also get this behaviour perfect -- Greg > > Parallel.Foreach<FileInfo, CryptoUtil>(files, parallelOptions, > () => > { > return new CryptoUtil(); > }, > (file, pls, index, util) => > { > byte[] buffer = read the file > long hash = util.HashBuffer(buffer); > return util; > }, > (util) => > { > util.Dispose(); > }); > >
