Any good async workflow patterns for C# 4?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-07 04:08:33

问题


Can anyone point me to some patterns for handling async workflows in C#? I know this is coming soon in C# 5 but I was wondering if anyone had already done an implementation in C# 4 that approximates the same effects?

Basically I'm writing a lot of async code in Silverlight 4 like this:

CallService(
    (service) => service.DoSomething(1, 2, 3),
    (response) =>
    {
        // some local code

        // need to call another service async
        CallService(
            (service) => service.DoSomethingElse(4, 5, 6),
            (response) =>
            {
                // even more nested async calls, you get the idea...
            }
        )
    }
);

The nested callbacks are getting to me. I really like the way F# provides async workflows to handle this, as you end up writing code that looks and feels synchronous, which is what I really want.


回答1:


We have implemented an async library based on the following set of articles:

http://www.codeproject.com/KB/silverlight/ConsumingWCFServiceWithou.aspx http://www.codeproject.com/KB/silverlight/FixingAllAsync.aspx

Pay particular attention to Part 2 of the article which describes how to simplify async WCF service calls. The author describes an approach to the consumption of asynchronous services based on coroutines. A fantastic article.




回答2:


You can try to use Caliburn Micro framework, which is MVVM framework, but it also provides an implementation for coroutines - it allows you to invoke async methods in a sequence way.

In general I highly recommend you reading about whole Caliburn Micro framework.

You can also read more about coroutines on Matt Hamilton blog




回答3:


Have you had a look at Reactive Extensions?

Works on SL and .NET and provides some really nice ways of having async code

http://msdn.microsoft.com/en-us/data/gg577609

Alternatively maybe look at F# which already has async in Silverlight 4. Heres a (cheeky) look....

http://www.trelford.com/blog/post/Exclusive-New-C-5-features-available-in-VS2010-SP1.aspx




回答4:


Jeremy Likness has a nice series of blog posts where he describes using iterators to process sequential asynchronous calls:

Part 1: Sequential Asynchronous Workflows in Silverlight using Coroutines

Part 2: Sequential Asynchronous Workflows Part 2: Simplified



来源:https://stackoverflow.com/questions/6529066/any-good-async-workflow-patterns-for-c-sharp-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!