Asynchronous webrequest taking 100 % CPU usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • archana

    #1

    Asynchronous webrequest taking 100 % CPU usage

    Hi all
    i am having application which is using asychronous web request.

    At a time i am processing 5 urls asynchronously.

    Application working properly for 5 asynchronous call. But sometimes CPU
    usage suddently increases to 100 %.

    Can some one tell me why this is happening.

    Any help will be truely appreciated.

    thanks in advance.

  • Rainier [MCT]

    #2
    RE: Asynchronous webrequest taking 100 % CPU usage

    Hi Archana,

    Web Requests are allways synchronous.

    This may be a bit a shocking answer but remember that the http protocol
    allways need a response 200 Ok or some kind or error.
    So lets say you use Javascript to do a post towards a webservice using the
    asynch switch. Although your code will continue to run untill the event
    requestcomplete (or something like that) hits.

    The line towards the webserver will remain open.

    If you really want to be asynchronous using the web start the server side
    processes in seperate treads. I think IE is looping through it's open
    connections increasing the CPU load to 100% This is when I found out the
    lines remained open :-)

    Hope this helped.

    Kind regards,

    --
    Rainier van Slingerlandt
    (Freelance trainer/consultant/developer)



    "archana" wrote:
    [color=blue]
    > Hi all
    > i am having application which is using asychronous web request.
    >
    > At a time i am processing 5 urls asynchronously.
    >
    > Application working properly for 5 asynchronous call. But sometimes CPU
    > usage suddently increases to 100 %.
    >
    > Can some one tell me why this is happening.
    >
    > Any help will be truely appreciated.
    >
    > thanks in advance.
    >
    >[/color]

    Comment

    • archana

      #3
      Re: Asynchronous webrequest taking 100 % CPU usage

      Hi

      thanks for your reply.

      I am using webrequest class sychronously. And i have C# desktop based
      application. Not using any server side scripting. In MSDN , one
      example is giving for making asynchronous request. That i am using
      which is taking lots of cpu usage if i increase asynchronous request
      from 1 to 5 request.

      I hope you will get my problem.

      Thanks


      Rainier [MCT] wrote:
      [color=blue]
      > Hi Archana,
      >
      > Web Requests are allways synchronous.
      >
      > This may be a bit a shocking answer but remember that the http protocol
      > allways need a response 200 Ok or some kind or error.
      > So lets say you use Javascript to do a post towards a webservice using the
      > asynch switch. Although your code will continue to run untill the event
      > requestcomplete (or something like that) hits.
      >
      > The line towards the webserver will remain open.
      >
      > If you really want to be asynchronous using the web start the server side
      > processes in seperate treads. I think IE is looping through it's open
      > connections increasing the CPU load to 100% This is when I found out the
      > lines remained open :-)
      >
      > Hope this helped.
      >
      > Kind regards,
      >
      > --
      > Rainier van Slingerlandt
      > (Freelance trainer/consultant/developer)
      > www.slingerlandt.com
      >
      >
      > "archana" wrote:
      >[color=green]
      > > Hi all
      > > i am having application which is using asychronous web request.
      > >
      > > At a time i am processing 5 urls asynchronously.
      > >
      > > Application working properly for 5 asynchronous call. But sometimes CPU
      > > usage suddently increases to 100 %.
      > >
      > > Can some one tell me why this is happening.
      > >
      > > Any help will be truely appreciated.
      > >
      > > thanks in advance.
      > >
      > >[/color][/color]

      Comment

      • Joerg Jooss

        #4
        Re: Asynchronous webrequest taking 100 % CPU usage

        Rainier [MCT] wrote:
        [color=blue]
        > Hi Archana,
        >
        > Web Requests are allways synchronous.
        >
        > This may be a bit a shocking answer but remember that the http
        > protocol allways need a response 200 Ok or some kind or error.[/color]

        You're confusing asynchronous I/O with connection-oriented protocols.
        HTTP isn't synchronous. Nor is it asynchronous. The term just doesn't
        apply here.

        Cheers,
        --

        mailto:news-reply@joergjoos s.de

        Comment

        • Joerg Jooss

          #5
          Re: Asynchronous webrequest taking 100 % CPU usage

          archana wrote:
          [color=blue]
          > Hi all
          > i am having application which is using asychronous web request.
          >
          > At a time i am processing 5 urls asynchronously.
          >
          > Application working properly for 5 asynchronous call. But sometimes
          > CPU usage suddently increases to 100 %.
          >
          > Can some one tell me why this is happening.
          >
          > Any help will be truely appreciated.[/color]

          Post your code, otherwise it's all just guessing.

          Cheers,
          --

          mailto:news-reply@joergjoos s.de

          Comment

          • archana

            #6
            Re: Asynchronous webrequest taking 100 % CPU usage

            Hi

            i am posting code of processing url asynchronously.

            using System;
            using System.Net;
            using System.Threadin g;
            using System.Text;
            using System.IO;

            // The RequestState class passes data across async calls.
            public class RequestState
            {
            const int BufferSize = 1024;
            public StringBuilder RequestData;
            public byte[] BufferRead;
            public WebRequest Request;
            public Stream ResponseStream;
            // Create Decoder for appropriate enconding type.
            public Decoder StreamDecode = Encoding.UTF8.G etDecoder();

            public RequestState()
            {
            BufferRead = new byte[BufferSize];
            RequestData = new StringBuilder(S tring.Empty);
            Request = null;
            ResponseStream = null;
            }
            }

            // ClientGetAsync issues the async request.
            class ClientGetAsync
            {
            public static ManualResetEven t allDone = new
            ManualResetEven t(false);
            const int BUFFER_SIZE = 1024;

            public static void Main(string[] args)
            {
            if (args.Length < 1)
            {
            showusage();
            return;
            }

            // Get the URI from the command line.
            Uri httpSite = new Uri(args[0]);

            // Create the request object.
            WebRequest wreq = WebRequest.Crea te(httpSite);

            // Create the state object.
            RequestState rs = new RequestState();

            // Put the request into the state object so it can be passed
            around.
            rs.Request = wreq;

            // Issue the async request.
            IAsyncResult r = (IAsyncResult) wreq.BeginGetRe sponse(
            new AsyncCallback(R espCallback), rs);

            // Wait until the ManualResetEven t is set so that the application

            // does not exit until after the callback is called.
            allDone.WaitOne ();

            Console.WriteLi ne(rs.RequestDa ta.ToString());
            }

            public static void showusage() {
            Console.WriteLi ne("Attempts to GET a URL");
            Console.WriteLi ne("\r\nUsage:" );
            Console.WriteLi ne(" ClientGetAsync URL");
            Console.WriteLi ne(" Example:");
            Console.WriteLi ne(" ClientGetAsync
            http://www.contoso.com/");
            }

            private static void RespCallback(IA syncResult ar)
            {
            // Get the RequestState object from the async result.
            RequestState rs = (RequestState) ar.AsyncState;

            // Get the WebRequest from RequestState.
            WebRequest req = rs.Request;

            // Call EndGetResponse, which produces the WebResponse object
            // that came from the request issued above.
            WebResponse resp = req.EndGetRespo nse(ar);

            // Start reading data from the response stream.
            Stream ResponseStream = resp.GetRespons eStream();

            // Store the response stream in RequestState to read
            // the stream asynchronously.
            rs.ResponseStre am = ResponseStream;

            // Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
            IAsyncResult iarRead = ResponseStream. BeginRead(rs.Bu fferRead, 0,

            BUFFER_SIZE, new AsyncCallback(R eadCallBack), rs);
            }


            private static void ReadCallBack(IA syncResult asyncResult)
            {
            // Get the RequestState object from AsyncResult.
            RequestState rs = (RequestState)a syncResult.Asyn cState;

            // Retrieve the ResponseStream that was set in RespCallback.
            Stream responseStream = rs.ResponseStre am;

            // Read rs.BufferRead to verify that it contains data.
            int read = responseStream. EndRead( asyncResult );
            if (read > 0)
            {
            // Prepare a Char array buffer for converting to Unicode.
            Char[] charBuffer = new Char[BUFFER_SIZE];

            // Convert byte stream to Char array and then to String.
            // len contains the number of characters converted to Unicode.
            int len =
            rs.StreamDecode .GetChars(rs.Bu fferRead, 0, read, charBuffer,
            0);

            String str = new String(charBuff er, 0, len);

            // Append the recently read data to the RequestData
            stringbuilder
            // object contained in RequestState.
            rs.RequestData. Append(
            Encoding.ASCII. GetString(rs.Bu fferRead, 0, read));

            // Continue reading data until
            // responseStream. EndRead returns -1.
            IAsyncResult ar = responseStream. BeginRead(
            rs.BufferRead, 0, BUFFER_SIZE,
            new AsyncCallback(R eadCallBack), rs);
            }
            else
            {
            if(rs.RequestDa ta.Length>0)
            {
            // Display data to the console.
            string strContent;
            strContent = rs.RequestData. ToString();
            }
            // Close down the response stream.
            responseStream. Close();
            // Set the ManualResetEven t so the main thread can exit.
            allDone.Set();
            }
            return;
            }
            }


            Only thing is in main instead of processing one url i have one array
            containing urls and i am starting processing of url at the same time.

            thanks.

            Comment

            Working...