开发者

Any good async workflow patterns for C# 4?

开发者 https://www.devze.com 2023-03-16 14:28 出处:网络
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 approx开发者_运

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 approx开发者_运维技巧imates 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.


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消