Problem with Asynchronous ASP.NET page - please help

D

davidpenty

Hi there,

I am having some problems with a multi-threaded asp.net seach page.
My search page sends off four asynchronous http requests to four search
engines then waits for the results to come back (in xml)
The searches are just URLs with querystrings.

Each request sets a boolean to true when it has got a reply from the
search engine. I have declare the booleans as 'volatile'.

The problem I have is that once every four or five searches one of the
requests times out. This isn't a network problem because I have access
to the logs on all of the servers and I can see the search engines
getting the url and I am pretty sure that the results are coming back.
I thought that this was down to the booleans that flag when the search
is complete being cached or something - that's why I declared them as
volatile. This made no difference.
I have a timeout of 10 seconds, the searches (when they do come back)
come back in less than a second


the relevant bits of the code are as follows ( I have removed the error
checking etc...):
I have only included 2 of the searches to save space.



volatile bool blnWebDone = false;
volatile bool blnILSDone = false;
int iTimeout = 10;

protected void Page_Load(object sender, EventArgs e)
{
RunSearches();
iTimeout = iTimeout * 2;
for (int i = 1; i <= iTimeout; i++)
{
Thread.Sleep(500);
if (blnWebDone && blnILSDone)
{
//all results have returned before the timeout
break;
}
}
}

private void RunSearches()
{
Uri WebSearch = new Uri(strWebURL);
HttpWebRequest wreqWeb =
(HttpWebRequest)WebRequest.Create(WebSearch);
IAsyncResult rWeb =
(IAsyncResult)wreqWeb.BeginGetResponse(new
AsyncCallback(this.WebSearchCallback), wreqWeb);

Uri ILSSearch = new Uri(strILSsearchURL);
HttpWebRequest wreqILS =
(HttpWebRequest)WebRequest.Create(ILSSearch);
IAsyncResult rILS =
(IAsyncResult)wreqILS.BeginGetResponse(new
AsyncCallback(this.ILSSearchCallback), wreqILS);
}

private void WebSearchCallback(IAsyncResult ar)
{
try
{
HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
HttpWebResponse resp =
(HttpWebResponse)req.EndGetResponse(ar);
Stream resStream = resp.GetResponseStream();
StreamReader r = new StreamReader(resStream);
XmlTextReader reader = new XmlTextReader(r);
while (reader.Read())
{
//Process the results...
}
}
catch (Exception e)
{
string strError = (e.ToString());
}
blnWebDone = true;
}

private void ILSSearchCallback(IAsyncResult ar)
{
try
{
HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
HttpWebResponse resp =
(HttpWebResponse)req.EndGetResponse(ar);
Stream resStream = resp.GetResponseStream();
StreamReader r = new StreamReader(resStream);
XmlTextReader reader = new XmlTextReader(r);
while (reader.Read())
{
//Process the results...
}
}
catch (Exception e)
{
string strError = (e.ToString());
}
blnILSDone = true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top