Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用asyncwait-c等待列表中的所有项目处理#_C#_.net_Asynchronous_Async Await_Task Parallel Library - Fatal编程技术网

C# 如何使用asyncwait-c等待列表中的所有项目处理#

C# 如何使用asyncwait-c等待列表中的所有项目处理#,c#,.net,asynchronous,async-await,task-parallel-library,C#,.net,Asynchronous,Async Await,Task Parallel Library,我试图用这段代码实现以下功能 1。我有一个项目列表,我希望以并行方式处理项目,以加快处理过程 2.我还想等到列表中的所有数据都得到处理,我需要在数据库中更新相同的内容 private async Task<bool> ProceeData<T>(IList<T> items,int typeId,Func<T, bool> updateRequestCheckPredicate, Func<T, bool> newRequestC

我试图用这段代码实现以下功能

1。我有一个项目列表,我希望以并行方式处理项目,以加快处理过程

2.我还想等到列表中的所有数据都得到处理,我需要在数据库中更新相同的内容

   private async Task<bool> ProceeData<T>(IList<T> items,int typeId,Func<T, bool> updateRequestCheckPredicate, Func<T, bool> newRequestCheckPredicate)
    {
            continueFlag = (scripts.Count > =12 ) ? true : false;
            await ProcessItems(items, updateRequestCheckPredicate, newRequestCheckPredicate);

            //Wait Until all items get processed and Update Status in database

            var updateStatus =UpdateStatus(typeId,DateTime.Now);
             return continueFlag;
    }

    private async Task ProcessItems<T>(IList<T> items,Func<T, bool> updateRequestCheckPredicate, Func<T, bool> newRequestCheckPredicate)
    {
       var itemsToCreate = items.Where(newRequestCheckPredicate).ToList();

        var createTask  = scripts
                     .AsParallel().Select(item => CrateItem(item);
                     .ToArray();
        var createTaskComplete = await Task.WhenAll(createTask);

        var itemsToUpdate = items.Where(updateRequestCheckPredicate).ToList();

        var updateTask = scripts
                   .AsParallel().Select(item => UpdateItem(item)
                   .ToArray();

        var updateTaskComplete = await Task.WhenAll(updateTask);
    }

     private async Task<ResponseResult> CrateItem<T>(T item)
    {
        var response = new ResponseResult();
         Guid requestGuid = Guid.NewGuid();
        auditSave = SaveAuditData(requestGuid);
        if (auditSaveInfo.IsUpdate)
        {
           response = await UpdateItem(item);
        }
        response = await CreateTicket<T>(item);
        // Wait response
        UpdateAuditData(response)
    }
    private async Task<ServiceNowResponseResult> CreateTicket<T>(T item)
    {
        // Rest call and need to wait for result
        var response = await CreateNewTicket<T>(scriptObj, serviceRequestInfo);
        return response;
    }
private async Task ProceeData(IList项、int-typeId、Func-updateRequestCheckPredicate、Func-newRequestCheckPredicate)
{
continueFlag=(scripts.Count>=12)?true:false;
等待ProcessItems(items、updateRequestCheckPredicate、newRequestCheckPredicate);
//等待所有项目得到处理并更新数据库中的状态
var updateStatus=updateStatus(typeId,DateTime.Now);
返回连续滞后;
}
专用异步任务ProcessItems(IList项、Func updateRequestCheckPredicate、Func newRequestCheckPredicate)
{
var itemsToCreate=items.Where(newRequestCheckPredicate.ToList();
var createTask=脚本
.AsParallel().Select(项=>板条箱项(项);
.ToArray();
var createTaskComplete=等待任务.WhenAll(createTask);
var itemsToUpdate=items.Where(updateRequestCheckPredicate.ToList();
var updateTask=脚本
.AsParallel().Select(项=>UpdateItem(项)
.ToArray();
var updateTaskComplete=wait Task.WhenAll(updateTask);
}
专用异步任务项(T项)
{
var response=newresponseResult();
Guid requestGuid=Guid.NewGuid();
auditSave=SaveAuditData(requestGuid);
if(auditSaveInfo.IsUpdate)
{
响应=等待更新项(项目);
}
响应=等待CreateTicket(项目);
//等待响应
UpdateAuditData(响应)
}
专用异步任务CreateTicket(T项)
{
//Rest调用,需要等待结果
var response=await CreateNewTicket(scriptObj,serviceRequestInfo);
返回响应;
}

我是一个新的等待异步概念的人,所以任何人都请告诉我我所做的是正确的方法还是错误的方法,请帮助我使用示例代码

所有这些
AsParallel
都不是必需的,但您需要将回调的签名更改为异步

这里有一个例子

    async Task ProcessAllItems<T>(IEnumerable<T> items,
        Func<T, Task<bool>> checkItem, // an async callback
        Func<T, Task> processItem)
    {
// if you want to group all the checkItem before any processItem is called
// then do WhenAll(items.Select(checkItem).ToList()) and inspect the result
// the code below executes all checkItem->processItem chains independently
        List<Task> checkTasks = items
            .Select(i => checkItem(i)  
            .ContinueWith(_ =>
            {
                if (_.Result)
                    return processItem(i);
                return null;
            }).Unwrap())   // .Unwrap takes the inner task of a Task<Task<>>
            .ToList();     // when making collections of tasks ALWAYS materialize with ToList or ToArray to avoid accudental multiple executions

        await Task.WhenAll(checkTasks);

    }

所有这些
AsParallel
都不是必需的,但您需要将回调的签名更改为异步

这里有一个例子

    async Task ProcessAllItems<T>(IEnumerable<T> items,
        Func<T, Task<bool>> checkItem, // an async callback
        Func<T, Task> processItem)
    {
// if you want to group all the checkItem before any processItem is called
// then do WhenAll(items.Select(checkItem).ToList()) and inspect the result
// the code below executes all checkItem->processItem chains independently
        List<Task> checkTasks = items
            .Select(i => checkItem(i)  
            .ContinueWith(_ =>
            {
                if (_.Result)
                    return processItem(i);
                return null;
            }).Unwrap())   // .Unwrap takes the inner task of a Task<Task<>>
            .ToList();     // when making collections of tasks ALWAYS materialize with ToList or ToArray to avoid accudental multiple executions

        await Task.WhenAll(checkTasks);

    }

我对你的示例代码有点困惑。这些方法是什么checkItem、ProcessItem。你能把它映射到我在原始问题中使用的函数吗?如果你用与我在原始问题中使用的相同的方法名和变量名重写代码,这将非常有帮助。itemsToUpdate集合的处理应该只在完成后开始处理createTask Collection您不应该使用
ContinueWith
来处理异步代码;而应该使用
wait
来代替。我对您的示例代码有点困惑。这些方法是什么checkItem,ProcessItem。请将其与我在原始问题中使用的函数映射。如果您使用我在原始问题中使用的相同方法名称和变量名称itemsToUpdate集合的处理应仅在完成createTask集合的处理后开始。对于异步代码,不应使用
ContinueWith
;而应使用
wait