Dot Net 4 TPL is a wonderful thing.. It helped me turn a 2minute task into about 15 seconds.
Here's some reading for those interested.. 6 articles by Sacha Barber on "The Code Project".. 1. Starting Tasks / Trigger Operations / Exception Handling / Cancelling / UI Synchronization <http://www.codeproject.com/KB/cs/TPL1.aspx> 2. Continuations / Cancelling Chained Tasks<http://www.codeproject.com/KB/cs/TPL2.aspx> 3. Parallel For / Custom Partioner / Aggregate Operations<http://www.codeproject.com/KB/cs/TPL3.aspx> 4. Parallel LINQ <http://www.codeproject.com/KB/threads/TPL4.aspx> 5. Pipelines <http://www.codeproject.com/KB/cs/Tasks5.aspx> 6. Advanced Scenarios / v.Next for Tasks<http://www.codeproject.com/KB/cs/Tasks6.aspx> On Sat, Sep 17, 2011 at 7:13 PM, Greg Keogh <[email protected]> wrote: > Folks, another heads-up ... I had some old hobby code that would scan > folders for image files, extract the details of the file and image and > update a SQLite DB to create an “index” of all my images. I haven’t run the > utility for a few years, but now I have about 4000 images and running it > afresh was taking at least 30 minutes to read all the files. It’s slow > because I have to read all the file bytes to load the Image and take an MD5 > hash of newly added files (it’s much faster on subsequent scans when it > knows most files haven’t changed).**** > > ** ** > > Coincidentally, the latest MSDN magazine has an article titled *The Past, > Present and Future of Parallelizing .NET > Applications<http://msdn.microsoft.com/en-us/magazine/hh335070.aspx> > * which reminded me of the System.Threading.Tasks.Parallel class which has > many For and ForEach methods.**** > > ** ** > > To use the Parallel class properly you have to discipline yourself to do > two things: (1) Make sure the method that does the “work” is IEnumerable(2) > Be thread safe by lock[ing] whatever the work method updates > (obviously).**** > > ** ** > > Thing 1 is the important because you must adjust your coding style to make > sure heavyweight methods are IEnumerable. Once you do that you can just go > Parallel.ForEach on the method and bingo it just works and it magically > runs parallelised.**** > > ** ** > > My image scan now takes about 10 minutes and in Task Manager I can see all > 6 CPUs pumping electrons.**** > > ** ** > > There are apparently simple techniques for cancelling parallelised work, > but I haven’t tried that yet.**** > > ** ** > > Cheers**** > > Greg**** > > ** ** > > ** ** >
