This is just processing urls' asynchronously. I am trying to process
Alvin Bruney [MVP] wrote:
> 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 > -------------------------------------------------------
>
>
> <trialproduct2004@yahoo.com> wrote in message
> news:1153830433.378766.38690@s13g2000cwa.googlegroups.com...
> > 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.
> >