c# - awaiting two things in a loop? -


how can await 2 or more things (with different types) @ same time? in event loop:

while(true) {     letter msg1 = await waitforletter();     //read msg1 , reply.     sms msg2 = await waitforsms();     //read msg2 , reply } 

that doesn't right. 2 messages end blocking each other?

currently, code wait each method finish in turn. if want send out each message wait both @ end, can use task.waitall method (assuming methods return task<t> object.

while(true) {     task<letter> msgtask1 = waitforletter();     task<sms> msgtask2 = waitforsms();      task.waitall(msgtask1, msgtask2); } 

you can result of each task result property (again assuming method return task<t>:

letter msg1 = msgtask1.result; sms msg2 = msgtask2.result; 

of course, assumes implementation of waitforletter , waitforsms independent , don't block each other.

if want wait any of tasks finish, can use `task.waitany' same end. returns index of task finished know 1 complete.

while(true) {     task<letter> msgtask1 = waitforletter();     task<sms> msgtask2 = waitforsms();      var finishedtask = task.waitany(msgtask1, msgtask2); } 

Comments