views:

51

answers:

3

Hi,

I recently asked a developer to write a library using the new multithreading capabilities of .NET 4.0.

He's done a great job, but I'm concerned that the Task logic is repeated throughout the code and not nicely encapsulated.

I'm also concerned that this creates a problem when it comes to testing. Normally, I test by creating a seam in the code by creating an interface and a stub/mock object to run my tests against.

I suppose this is possible using this way of doing things. It just seems that the production code logic would be very different than the test logic.

Is the solution to make tests that are parallel as well, and just repeat the Task logic in them? Or have people thought up patterns where much of the Task handling logic can be encapsulated for easy duplication?

Thanks!

Task task = Task.Factory.StartNew(() =>
        {
            if(cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException();
            }

            if (_bookHeader.EncryptionType != 0)
            {
                throw new MobiException("The book is encrypted");
            }

            ExtractText();

            partReady(66.66f);
        }, cancellationToken);

        Task opfTask = task.ContinueWith(antecedent =>
            {
                if (antecedent.Status != TaskStatus.Canceled)
                {
                    OpfDocument opf = CreateOpf();

                    partReady(80);

                    MobiDocument book = new MobiDocument()
                    {
                        Contents = _mobiHtml,
                        Description = opf,
                        Header = _bookHeader,
                        Sections = _sections
                    };
                    Document = book;


                    GC.Collect();

                    partReady(100);
                }
            });             

        return opfTask;
    }
+1  A: 

Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4

Updated samples for parallel programming

A Tour Through the Parallel Programming Samples for .NET 4

Parallel Programming with .NET

Mitch Wheat
+1 Not trying to be a smart ace, but this thread is another illustration of the problem. I havent downvoted the other post, but I'm not upvoting it either (Seriously, dont want to get into any arguments here - have starred this and will be coming back to read your recommends)
Ruben Bartelink
A: 

Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4

This document provides a detailed and in-depth tour of support in the Microsoft® .NET Framework 4 for parallel programming. This includes an examination of common parallel patterns and how they’re implemented without and with this new support in the .NET Framework, as well as covering best practices for developing parallel components utilizing parallel patterns.

Leniel Macaferi
Surely that's a later duplicate of @Mitch Wheat's answer? Any chance you can remove?
Ruben Bartelink
@Ruben Bartelink: people sometimes post at the same time. I think it's pointless trying to penalise people for being helpful: SO will cease to exist if that's the prevailing wind...
Mitch Wheat
*NOT* suggesting penalising, suggesting not rewarding such accidents (I'm not suggesting that Leniel has either a) copied yours or b) failed to press Ctrl-R or F5 enough times and should do so). Not trying to upset anyone. I know sorting by ovte moves such stuff which 'isnt hurting anyone' down the bottom, but I personally would prefer it not to be there. Seccond sentiment of making it a community witout silly backbiting and sorry for amount of wasted time on Friday :(
Ruben Bartelink
When I posted this answer Mitch's answer didn't have the link I posted. SO gives you a time frame to update your post. This way Mitch' answer doesn't show the update.
Leniel Macaferi
A: 

I'd be surprised (but delighted) if it's possible to better the last 3 chapters of CLR via C# 3rd Ed by Richter for getting a great handle on the landscape of parallel stuff in .NET 4. (Just read Effective C#, Second Edition and can't say the same for it's coverage of TPL and PL)

One reason I feel this is relevant is that he talks a lot about how to make the code clean and maintainable (and some stuff in PowerThreading that he's used quite a bit). (And in the unlikely event you're not aware of the book, it's the CLR book to have, and the 3rd Ed is a significant upgrade of the 2nd Ed)

Ruben Bartelink