c# - Awaiting multiple tasks in a loop -


i need call webservice multiple times because has limit of 100 objects returned per call, , want in parallel.

i'm making task each web service call , stop when task returns less limit of items, means there no more items get.

public static async task<list<space>> getspacesbytypeparallel(spacetype type, string ticket, int degreeofparallelism) {     int offset = 0;     int batchsize = degreeofparallelism * return_limit;     list<space> spaces = new list<space>();     task<list<space>>[] tasks = new task<list<space>>[degreeofparallelism];     bool shouldcontinue = true;     while(shouldcontinue)     {         for(int = 0; < degreeofparallelism; i++)         {             tasks[i] = task.run<list<space>>( () => getspacesatoffset(offset + (i * return_limit), return_limit, ticket, null, type.tostring()) ); //getspacesatoffset synchronous method         }          list<space>[] result = await task.whenall(tasks);          foreach(list<space> item in result)         {             spaces.addrange(item);             if(item.count < return_limit)             {                 shouldcontinue = false;             }         }          offset += batchsize;     }     return spaces; } 

i'm running synchronously testing purposes:

var spaces = space.getspacesbytype(spacetype.type1, ticket).result; 

however returns empty list, if step through debugger should.

what doing wrong ?

i believe it's caused closure variable. try change

    for(int = 0; < degreeofparallelism; i++)     {         var n = i;         tasks[i] = task.run<list<space>>( () => getspacesatoffset(offset + (n * return_limit), return_limit, ticket, null, type.tostring()) ); //getspacesatoffset synchronous method     } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -