异步函数进行异步调用

时间:2011-08-23 10:47:26

标签: c# .net multithreading c#-3.0 iasyncresult

我有一个使用BeginXxx EndXxx异步模式的库(显然,以下代码已经简化):

ILibrary
{
    IAsyncResult BeginAction(string name, AsyncCallback callback, object state);
    int EndAction(IAsyncResult asyncResult, out object state)
}

我正在尝试构建一个使用该库的类,并且它本身实现了相同的模式:

Manager
{
    private ILibrary library;

    public IAsyncResult BeginMultipleActions(IEnumerable<string> names, 
                                             AsyncCallback callback, object state)
    {
        var handles = new List<IAsyncResult>();
        foreach(string name in names)
        {
            var handle = library.BeginAction(name, OnSingleActionCompleted, null);
            handles.Add(handle);
        }
        //???
    }
    public int EndMultipleActions(IAsyncResult asyncResult, out object state)
    {
        //???
    }

    private void OnSingleActionCompleted(IAsyncResult asyncResult)
    {
        //???
    }
}

我尝试了几种解决方案(使用ManualResetEvent),但我发现我的代码非常难看。在没有丢失功能(回调,等待句柄,......)的情况下,最简单的方法(在C#3.5中)是什么?

2 个答案:

答案 0 :(得分:1)

为了能够成功实现您描述的模式,您应该能够在所有单个操作完成后调用传递到BeginMultipleActions方法的回调。使用事件并等待它们可能会破坏您执行异步调用的唯一目的,因为它们会阻塞线程。您传递给单个操作的回调应该能够确定何时完成所有方法,然后将客户端提供的回调调用BeginMultipleActions方法。这是一个可能为你做的样本(我还没有测试过)。但是,这只是为了让你开始并且远非完美。您将需要处理几个细微差别。

interface ILibrary
{
    IAsyncResult BeginAction(string name, AsyncCallback callback, object state);
    int EndAction(IAsyncResult asyncResult, out object state);
}

class Manager
{
    private ILibrary library;

    class AsyncCallInfo : IAsyncResult
    {
        public int PendingOps;
        public AsyncCallback Callback { get; set; }
        public object AsyncState { get; set; }

        public WaitHandle AsyncWaitHandle
        {
            // Implement this if needed
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return PendingOps == 0; }
        }
    }

    public IAsyncResult BeginMultipleActions(IEnumerable<string> names,
                                             AsyncCallback callback, object state)
    {
        var callInfo = new AsyncCallInfo {
            Callback = callback,
            AsyncState = state
        };

        callInfo.PendingOps = names.Count();
        foreach (string name in names)
        {
            library.BeginAction(name, ar => OnSingleActionCompleted(ar, callInfo), null);
        }

        return callInfo;
    }

    public int EndMultipleActions(IAsyncResult asyncResult, out object state)
    {
        // do your stuff
        state = asyncResult.AsyncState;
        return 0;
    }

    private void OnSingleActionCompleted(IAsyncResult asyncResult, AsyncCallInfo callInfo)
    {
        //???
        Interlocked.Decrement(ref callInfo.PendingOps);
        if (callInfo.PendingOps == 0)
        {
            callInfo.Callback(callInfo);
        }
    }
}

我建议你看一下Jeffrey Richter的AsyncEnumerator。它简化了编写异步代码的过程,并支持多种异步实现模式 - http://msdn.microsoft.com/en-us/magazine/cc546608.aspx

答案 1 :(得分:0)

使用beginInvok启动asincronous操作。它支持回调。

更多细节:

http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx