如何并行执行这个异步操作

问题描述 投票:0回答:1

在我的应用程序中,我有一个这样的方法:

private async Task LoadDataSetsAsyncOld()
{
    if (Cache["QISRUID317"+Session.SessionID]==null)
    {
        using (IParameterProviderBusinessLogic parameterProviderBusiness = new ParameterProviderBusinessLogic())
        {
            dataSet = await parameterProviderBusiness.GetOutlierParametersAsync().ConfigureAwait(false);
        
            using (ILocationProviderBusinessLogic _locationProviderBusinessLogic = new LocationProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                dataSet.Merge(await _locationProviderBusinessLogic.GetLocationProviderAsync());
            }

            using (ISamplePointProviderBusinessLogic _samplePointProviderBusinessLogic = new SamplePointProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                dataSet.Merge(await _samplePointProviderBusinessLogic.GetSamplePointProviderAsync());
            }

            using (IPMDOperationsProviderBusinessLogic operationsProviderDataAccess = new PMDOperationsProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                dataSet.Merge(await operationsProviderDataAccess.GetPMDOperationsAsync());
            }

            using (IMethodOfMeasureProviderBusinessLogic methodOfMeasureProviderBusinessLogic = new MethodOfMeasureProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                dataSet.Merge(await methodOfMeasureProviderBusinessLogic.GetMethodOfMeasureAsync().ConfigureAwait(false));
            }

            using (IPackerProviderBusinessLogic packerProviderBusinessLogic = new PackerProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                DataSet packerDs = await packerProviderBusinessLogic.GetPackerProviderAsync().ConfigureAwait(false);
                packerDs.Relations.RemoveAt(0);
                packerDs.AcceptChanges();
                dataSet.Merge(packerDs);
            }

            using (IFilterTypeProviderBusinessLogic filterTypeProviderBusinessLogic = new FilterTypeProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                dataSet.Merge(await filterTypeProviderBusinessLogic.GetFilterTypeProviderAsync().ConfigureAwait(false));
            }

            using (IFilterMakerProviderBusinessLogic _filterMakerProviderBusinessLogic = new FilterMakerProviderBusinessLogic((BusinessLogicBase)parameterProviderBusiness))
            {
                DataSet dsMaker = await _filterMakerProviderBusinessLogic.GetFilterMakerProviderAsync();
                dsMaker.Relations.RemoveAt(0);
                dsMaker.AcceptChanges();
                dataSet.Merge(dsMaker);
            }
        }

        Cache["QISRUID317" + Session.SessionID] = dataSet;
    }
}

我需要提高性能,所以我打算使用

Task.WhenAll()
并发执行方法中的任务。

但是我该怎么做,是否可以这样做,这是好的做法吗?

c# asynchronous task
1个回答
0
投票

像这样

var tasks = new List<Task>();

var dataSetTask = parameterProviderBusiness.GetOutlierParametersAsync()
                                                 .ConfigureAwait(false);

using (var _locationProviderBusinessLogic = new LocationProviderBusinessLogic(
                                         (BusinessLogicBase)parameterProviderBusiness))
using (var _samplePointProviderBusinessLogic = new SamplePointProviderBusinessLogic(
                                         (BusinessLogicBase)parameterProviderBusiness))
//other usings here
{
    var locationProviderTask = _locationProviderBusinessLogic
                                                .GetLocationProviderAsync();
    tasks.Add(locationProviderTask);

    var samplePointProviderTask = _samplePointProviderBusinessLogic
                                                .GetSamplePointProviderAsync();
    tasks.Add(samplePointProviderTask);

    //other requests here: run task, store task in variable and add into the list

    await Task.WhenAll(tasks);

    dataSet.Merge(await locationProviderTask);
    dataSet.Merge(await samplePointProviderTask);
    //other merges and results processing here.
}
© www.soinside.com 2019 - 2024. All rights reserved.