none
How to iterate through each task once they are in TaskPool

    Question

  • Hi Team,

    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. 

    Here is some code snippet.

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

    Previously the code was like below:

    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.



    Regards, Brijesh Shah

    Friday, March 31, 2017 11:39 AM

All replies

  • 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.

    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.

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

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, March 31, 2017 3:16 PM
    Moderator