一旦任务进入TaskPool,如何遍历每个任务

编程入门 行业动态 更新时间:2024-10-16 15:58:49
本文介绍了一旦任务进入TaskPool,如何遍历每个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,

我有以下方法,用户可以上传多个文件,而在单个请求中,我曾经获取这些文件.流程是我需要遍历每个流程,然后检查并执行一些流程.毕竟,我需要回馈有意义的信息.

I have following method where user can upload multiple files and in single request I used to get those files. process is I need to iterate through each of them and check and do some process. after all, I need to give a meaningful message back.

这是一些代码段.

public async Task<ActionResult> FileImport(FormCollection form) { // removed some code fore brevity ... var fileUploadTasks = new List<Task>(); for (int j = 0; j < Request.Files.AllKeys.Count(); j++) { HttpPostedFileBase file = (HttpPostedFileBase)Request.Files[j]; if(file.ContentLength > 200 MB) { fileUploadTasks.Add(Task.Run()=>DoOfflineProcessing(file)); } else { fileUploadTasks.Add(Task.Run()=>DoOnlineProcessing(file)); } } Task.WaitAll(fileUploadTasks.ToArray()); }

以前的代码如下:

public async Task<ActionResult> FileImport(FormCollection form) { // removed some code fore brevity ... for (int j = 0; j < Request.Files.AllKeys.Count(); j++) { HttpPostedFileBase file = (HttpPostedFileBase)Request.Files[j]; if(file.ContentLength > 200 MB) { string jsonresult = DoOfflineProcessing(file); } else { string jsonresult = DoOnlineProcessing(file); } return Content(jsonResult, "text/plain"); } }

我不明白如何在完成各个过程后为每个文件提供内容.

I don't understand How to provide the content for each individual file back when they complete the process.

关于Brijesh Shah,

Regards, Brijesh Shah

推荐答案

我认为您要提出的问题与您提出的主题不符.您正在创建一堆任务并将其放入列表中.因此,在回答您的主题问题时,该列表是您如何访问创建的任务的方法.

I think the question you're asking doesn't line up with the subject you gave. You are creating a bunch of tasks and putting them into a list. Therefore, in answer to your subject question, the list is how you get access to the tasks you created.

关于如何访问结果,只要等待结束,您只需要使用每个任务的Result属性.请记住,如果任务引发异常,则在调用Result时会触发该事件.

As for how you get access to the results then you simply need to use the Result property of each of the tasks once the wait is over. Keep in mind that if the task throws an exception it'll trigger when you call Result.

您使用的返回值是ActionResult,这意味着这是MVC.我忽略了那部分,因为它与手头的问题无关.您要问的问题是如何获得结果.由于您有任务,每个任务都会返回 您想要获取IEnumerable< string>的字符串结果.如何将其变形为最终的ActionResult取决于您.

The return value you're using is ActionResult which means this is MVC. I'm ignoring that part as it has nothing to do with the problem at hand. The problem you're asking about is how to get the results back. Since you have the tasks and each task returns a string you'll want to get the IEnumerable<string> results. How you morph that into your final ActionResult is up to you.

public async Task<ActionResult> FileImport ( FormCollection form ) { var fileUploadTasks = new List<Task>(); ... Task.WaitAll(fileUploadTasks.ToArray()); //Get the results of each task (as IEnumerable<string>) var results = from t in fileUploadTasks select t.Result; //Do any morphing to get it into ActionResult return ... }

迈克尔·泰勒 www.michaeltaylorp3

Michael Taylor www.michaeltaylorp3

更多推荐

一旦任务进入TaskPool,如何遍历每个任务

本文发布于:2023-11-23 20:25:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   TaskPool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!