I am new in Task Based programming. I have to call a WCF service Asynchronously and start a Task to wait for it as I do not want to wait in the Same Method so I have written code something like this
void MyMainMethod()
{
for(int j=0;j<10; j++)
{
int I = 100;
Task<response> res = _client.GetValueAsnc(request);
//Do some work
Task.Run(()=> WaitForResponse(res , I));
}
}
Async Task WaitForResponse(Task<response> res , int I)
{
await res;
if(res.responsecode == "Success")
{
//udatateDB...
}
else
{
//update DB with Error Message..
}
}
That way, If I call this service 10 times in a loop, it will start 10 tasks and give me response and instead of waiting for response in MyMainMethod() I am starting a separate task for each request.
Please let me know if this is correct approach or I am doing some major mistake here and also in case more details are required from my side to explain the question.
Thanks in Advance.