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();
});

Reply via email to