是否有人知道使用.NET 4.0的任务很好的资源,并行编程模式/测试? [英] Does anybody know of good resources for parallel programming patterns/testing using .NET 4.0 tasks?

查看:107
本文介绍了是否有人知道使用.NET 4.0的任务很好的资源,并行编程模式/测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我问一个开发商使用.NET 4.0的新的多线程能力来写一个图书馆。

他做了很多工作,但我担心的任务逻辑重复整个code和不很好地封装。

我也担心,这造成了一个问题,当谈到测试。通常情况下,我测试通过创建一个接口和存根/模拟对象,以对运行我的测试中创造了code一条缝。

我想这是可能使用这种方式做事。这似乎只是生产code逻辑会比测试逻辑非常不同的。

时的解决方案,使测试,平行,以及,重复刚才的任务逻辑呢?还是让人们想出来的图案,其中多任务处理逻辑可以概括为简单的重复?

谢谢!

 工作任务= Task.Factory.StartNew(()=>
        {
            如果(cancellationToken.IsCancellationRequested)
            {
                抛出新OperationCanceledException();
            }

            如果(_bookHeader.EncryptionType!= 0)
            {
                抛出新MobiException(书是加密);
            }

            ExtractText();

            partReady(66.66f);
        }的CancellationToken);

        任务opfTask = task.ContinueWith(前提=>
            {
                如果(antecedent.Status!= TaskStatus.Canceled)
                {
                    OpfDocument OPF = CreateOpf();

                    partReady(80);

                    MobiDocument书=新MobiDocument()
                    {
                        内容= _mobiHtml,
                        说明= OPF,
                        标题= _bookHeader,
                        第= _sections
                    };
                    文档=书;


                    所以GC.Collect();

                    partReady(100);
                }
            });

        返回opfTask;
    }
 

解决方案

<一个href="http://www.microsoft.com/downloads/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en"相对=nofollow>模式的并行编程:了解和应用并行模式与.NET Framework 4

更新了并行编程的样品

一次周游并行编程示例为.NET 4

并行编程与.NET

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

解决方案

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

这篇关于是否有人知道使用.NET 4.0的任务很好的资源,并行编程模式/测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆