一旦任务进入TaskPool,如何遍历每个任务 [英] How to iterate through each task once they are in TaskPool

查看:122
本文介绍了一旦任务进入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 ...
}

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于一旦任务进入TaskPool,如何遍历每个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆