Groups | Blog | Home
all groups > dotnet performance > july 2006 >

dotnet performance : Asynchronous web request not working properly.


trialproduct2004 NO[at]SPAM yahoo.com
7/25/2006 5:27:13 AM
hi all,

I am having application which i downloaded from net and modified some
part.

I am posting my code :-
private static int mrunning = 0;
static void Main(string[] args)

{

ArrayList alSites= new ArrayList() ;

alSites.Add("http://www.blah.com") ; // (yes apparently it's a real
site)

alSites.Add("http://msn.com") ;

alSites.Add("http://asp.net") ;

alSites.Add("http://microsoft.com") ;

alSites.Add("http://www.hello.com");// (yup, that's a site too)

ScanSites(alSites);

Console.ReadLine();
}



private static void ScanSites ( ArrayList sites)
{
for(int i =0 ; i< 500; i++)
{
Console.WriteLine("loop = " + i.ToString());
foreach (string uriString in sites)
{
System.Threading.Monitor.Enter (mrunning);
{
mrunning++;
}
System.Threading.Monitor.Exit(mrunning);
WebRequest request = HttpWebRequest.Create(uriString);
request.Method = "GET";
object data= new object(); //container for our "Stuff"
// RequestState is a custom class to pass info to the callback
RequestState state = new RequestState(request,data,uriString);

IAsyncResult result = request.BeginGetResponse(
new AsyncCallback(UpdateItem),state);

//Register the timeout callback
ThreadPool.RegisterWaitForSingleObject(
result.AsyncWaitHandle,
new WaitOrTimerCallback(ScanTimeoutCallback),
state,
(10* 1000), // 30 second timeout
true
);
}
}


private static void UpdateItem (IAsyncResult result)
{
try
{
// grab the custom state object
System.Threading.Monitor.Enter (mrunning);
{
mrunning--;
}
System.Threading.Monitor.Exit(mrunning);

RequestState state = (RequestState)result.AsyncState;
WebRequest request = (WebRequest)state.Request;
// get the Response

HttpWebResponse response =
(HttpWebResponse )request.EndGetResponse(result);

Console.WriteLine("Read: "+ state.SiteUrl + " " +
DateTime.Now.ToString() );
}
catch(Exception expt)
{
System.Threading.Monitor.Enter (mrunning);
{
mrunning--;
}
System.Threading.Monitor.Exit(mrunning);
Console.WriteLine(expt.Message);
}

}



private static void ScanTimeoutCallback (
object state, bool timedOut)
{

if (timedOut)
{
RequestState reqState = (RequestState)state;
if (reqState != null)
reqState.Request.Abort();
Console.WriteLine("aborted- timeout") ;
System.Threading.Monitor.Enter (mrunning);
{
mrunning--;
}
System.Threading.Monitor.Exit(mrunning);
}

}

For first 3-4 iteration this code is working properly. But then
afterwards it continuously showing msg 'aborted timeout'.

Can someone tell me why this is happening.

And this code is hanging sometimes. i don't know why this is happening.

Is there anything wrong in above code. Please correct me if i am
wrong.

Any help will be truely appreciated.

Thanks in advance.
Alvin Bruney [MVP]
7/25/2006 9:07:14 PM
there's a lot that's wrong with this code but that's what you get for
jumping on the bed, copying code from the web or whatever the saying goes.

You can't decrement in the exception block, that will throw the counter off.
It's very likely that the calls can timeout and cause exceptions.

it's hard to say more of what is wrong, your code doesn't compile and i've
spent the greater part of a minute trying but i've finally given up.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


[quoted text, click to view]

trialproduct2004 NO[at]SPAM yahoo.com
7/25/2006 11:22:17 PM
Hi,

sorry but i am not getting what error u are getting in this code.

You can run this code by removig mrunning counter also.

I just added this counter to get idea about how many asynchronous calls
are in progress.

This is just processing urls' asynchronously. I am trying to process
webrequest asynchronously.

You can get this example either from site :-

http://www.eggheadcafe.com/articles/20060120.asp

or in MSDN also same example is given.

Can u tell me what is wrong in it.

Thanks.

[quoted text, click to view]
Joerg Jooss
7/29/2006 1:17:38 PM
Thus wrote archana,

[quoted text, click to view]

HttpWebResponse is an IDisposable, bit you never Close() them.

Cheers,
--
Joerg Jooss
news-reply@joergjooss.de

trialproduct2004 NO[at]SPAM yahoo.com
8/3/2006 12:05:44 AM
Hi,
Thanks for your reply.

AFter calling close on httpwebresponse also my code is hanging. It is
not working properly.

Can you tell me what is wrong with this code.

Thanks

T
[quoted text, click to view]
AddThis Social Bookmark Button