在C#中将异步方法添加到类中?

时间:2011-08-15 01:44:30

标签: c# asynchronous

如何使用Async方法改进我的课程?

示例,WCF服务支持将Async方法添加到它们生成的类中,并且在UI中我只需要调用完整的事件。

我尝试搜索详细信息,但找不到任何相关信息。

任何人都可以提供一些示例代码吗?

非常感谢

2 个答案:

答案 0 :(得分:4)

Asynchronous Programming Overview

Asynchronous Programming Design Patterns

  

使用IAsyncResult设计模式的异步操作是   实现为名为BeginOperationName和的两个方法   EndOperationName开始和结束异步操作   OperationName分别为。例如,FileStream类提供   BeginRead和EndRead方法从a异步读取字节   文件。这些方法实现了Read的异步版本   方法

答案 1 :(得分:1)

这是一个非常快速且非常强大的示例,我将它放在一起,这是我之前使用过的模板:

public interface IResponse
{
    string ResponseCode { get; }
}

public sealed class Response : IResponse
{
    private readonly string responseCode;

    private Response(string responseCode)
    {
        this.responseCode = responseCode;
    }

    public string ResponseCode { get { return this.responseCode; } }

    public static IResponse Create(string responseCode)
    {
        return new Response(responseCode);
    }
}

public sealed class DoItCompletedEventArgs : AsyncCompletedEventArgs
{
    private readonly IResponse response;

    public DoItCompletedEventArgs(
        Exception error,
        bool canceled,
        object userState,
        IResponse response) : base(error, canceled, userState)
    {
        this.response = response;
    }

    public IResponse Response { get { return this.response; } }
}

public interface IDoStuff
{
    event EventHandler<DoItCompletedEventArgs> DoItCompleted;

    bool CanProcessAsynchronously { get; }

    IResponse DoIt(string[] args);

    void DoItAsync(string[] args);
}

public sealed class DoStuff : IDoStuff
{
    private delegate IResponse DoItDelegate(string[] args);

    public event EventHandler<DoItCompletedEventArgs> DoItCompleted;

    public bool CanProcessAsynchronously { get { return true; } }

    private DoStuff()
    {
    }

    public static IDoStuff Create()
    {
        return new DoStuff();
    }

    public IResponse DoIt(string[] args)
    {
        return Response.Create(args.Aggregate(string.Empty, (current, arg) => current + arg));
    }

    public void DoItAsync(string[] args)
    {
        DoItDelegate doIt = this.DoIt;

        doIt.BeginInvoke(args, this.DoDoItCompleted, doIt);
    }

    private void DoDoItCompleted(IAsyncResult result)
    {
        if (result == null)
        {
            return;
        }

        var doIt = result.AsyncState as DoItDelegate;

        if (doIt == null)
        {
            return;
        }

        var response = doIt.EndInvoke(result);
        var doItCompleted = this.DoItCompleted;

        if (doItCompleted != null)
        {
            doItCompleted(this, new DoItCompletedEventArgs(null, false, null, response));
        }
    }
}

internal static class Program
{
    private static void Main()
    {
        var doStuff = DoStuff.Create();

        if (doStuff.CanProcessAsynchronously)
        {
            var response = doStuff.DoIt(new[] { "stuff 1 ", "more stuff 1" });

            Console.WriteLine(response.ResponseCode);
        }
        else
        {
            doStuff.DoItCompleted += DoItCompleted;
            doStuff.DoItAsync(new[] { "stuff 2 ", "more stuff 2" });
        }

        Console.ReadLine();
    }

    private static void DoItCompleted(object sender, DoItCompletedEventArgs e)
    {
        Console.WriteLine(e.Response.ResponseCode);
    }
}