Multiple asynchronous method calls to method while in a loop
Posted
by ranabra
on Stack Overflow
See other posts from Stack Overflow
or by ranabra
Published on 2010-03-12T13:49:47Z
Indexed on
2010/03/12
14:07 UTC
Read the original article
Hit count: 592
I have spent a whole day trying various ways using 'AddOnPreRenderCompleteAsync' and 'RegisterAsyncTask' but no success so far.
I succeeded making the call to the DB asynchronous using 'BeginExecuteReader' and 'EndExecuteReader' but that is missing the point. The asynch handling should not be the call to the DB which in my case is fast, it should be afterwards, during the 'while' loop, while calling an external web-service.
I think the simplified pseudo code will explain best:
(Note: the connection string is using 'MultipleActiveResultSets')
"Select ID, UserName from MyTable"
'Open connection to DB
ExecuteReader();
if (DR.HasRows)
{
while (DR.Read())
{
'Call external web-service
'and get current Temperature of each UserName - DR["UserName"].ToString()
'Update my local DB
Update MyTable set Temperature = ValueFromWebService where UserName = DR["UserName"]
CmdUpdate.ExecuteNonQuery();
}
'Close connection etc
}
Accessing the DB is fast. Getting the returned result from the external web-service is slow and that at least should be handled Asynchnously.
If each call to the web service takes just 1 second, assuming I have only 100 users it will take minimum 100 seconds for the DB update to complete, which obviously is not an option.
There eventually should be thousands of users (currently only 2).
Currently everything works, just very synchnously :)
Thoughts to myself:
Maybe my way of approaching this is wrong?
Maybe the entire process should be called Asynchnously
Many thanx
© Stack Overflow or respective owner