日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

.Net通過TaskFactory.FromAsync簡化APM_實用技巧

作者:天方 ? 更新時間: 2022-08-10 編程語言

異步執行 I/O 密集型操作是生產高響應和可伸縮應用程序及組件的關鍵??勺屇褂脴O少量的線程來執行大量的工作,而無需阻止任何線程。然而異步編程卻有些麻煩,許多程序員不愿意去做它。

網上有不少通過lambda 表達式和AsyncEnumerator 等來實現簡化異步編程的方法,這些方法也確實行之有效,但在.net 4.0中,我們又多了一種選擇——通過TaskFactory.FromAsync簡化APM。

TaskFactory.FromAsync這個方法非常簡單,通過它可以把一個異步的任務轉換為一個Task,首先我們來看一個簡單的例子吧:

    static IEnumerable<Task> CopyStreamAsync(Stream input, Stream output)
    {
        var buffer = new byte[0x2000];
        while (true)
        {
            var readTask = Task<int>.Factory.FromAsync(input.BeginRead, input.EndRead, buffer, 0, buffer.Length, null);
            yield return readTask;

            if (readTask.Result == 0)
                break;

            yield return Task.Factory.FromAsync(output.BeginWrite, output.EndWrite, buffer, 0, buffer.Length, null);

        }
    }

這個例子通過TaskFactory.FromAsync把一系列異步操作轉換為了一個任務列。雖然這些都是異步操作,但在函數中卻和同步操作一樣直觀,十分簡單而清晰。

轉換為了的任務列后,我們就需要來執行這一系列任務,最簡單的方法如下:

    foreach (var task in CopyStreamAsync(input, output))
    {
        task.Wait();
    }

這種方式雖然直接有效,但它卻是一種阻塞式的操作,沒有達到異步的目的,我們一般可以通過如下方式把這個任務列轉換為一個任務,從而實現異步執行。

    public static Task Iterate(this TaskFactory factory, IEnumerable<Task> asyncIterator)
    {
        var scheduler = factory.Scheduler ?? TaskScheduler.Current;

        // Get an enumerator from the enumerable
        var enumerator = asyncIterator.GetEnumerator();
        if (enumerator == null) throw new InvalidOperationException();

        // Create the task to be returned to the caller. And ensure
        // that when everything is done, the enumerator is cleaned up.
        var trs = new TaskCompletionSource<object>(factory.CreationOptions);
        trs.Task.ContinueWith(_ => enumerator.Dispose(), scheduler);

        // This will be called every time more work can be done.
        Action<Task> recursiveBody = null;
        recursiveBody = antecedent =>
        {
            try
            {
                // If the previous task completed with any exceptions, bail
                if (antecedent != null && antecedent.IsFaulted)
                    trs.TrySetException(antecedent.Exception);

                else if (trs.Task.IsCanceled) trs.TrySetCanceled();

                else if (enumerator.MoveNext())
                    enumerator.Current.ContinueWith(recursiveBody, scheduler);

                // Otherwise, we're done!
                else trs.TrySetResult(null);
            }
            // If MoveNext throws an exception, propagate that to the user
            catch (Exception exc) { trs.TrySetException(exc); }
        };

        // Get things started by launching the first task
        factory.StartNew(_ => recursiveBody(null), scheduler);

        // Return the representative task to the user
        return trs.Task;
    }

這個函數我是摘錄自ParallelProgrammingSamples中的,里面還有好幾種其它的調用形式,可以根據需要選擇合適的方法。

原文鏈接:https://www.cnblogs.com/TianFang/archive/2009/11/07/1597953.html

欄目分類
最近更新