欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net通過TaskFactory.FromAsync簡化APM

 更新時間:2022年06月17日 09:50:08   作者:天方  
這篇文章介紹了.Net通過TaskFactory.FromAsync簡化APM的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

異步執(zhí)行 I/O 密集型操作是生產(chǎn)高響應(yīng)和可伸縮應(yīng)用程序及組件的關(guān)鍵。可讓您使用極少量的線程來執(zhí)行大量的工作,而無需阻止任何線程。然而異步編程卻有些麻煩,許多程序員不愿意去做它。

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

TaskFactory.FromAsync這個方法非常簡單,通過它可以把一個異步的任務(wù)轉(zhuǎn)換為一個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把一系列異步操作轉(zhuǎn)換為了一個任務(wù)列。雖然這些都是異步操作,但在函數(shù)中卻和同步操作一樣直觀,十分簡單而清晰。

轉(zhuǎn)換為了的任務(wù)列后,我們就需要來執(zhí)行這一系列任務(wù),最簡單的方法如下:

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

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

    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;
    }

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

到此這篇關(guān)于.Net通過TaskFactory.FromAsync簡化APM的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論