472,958 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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.

Dec 7 '05 #1
5 5506
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:
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.

Dec 7 '05 #2
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:
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:
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.


Dec 8 '05 #3
Rainier [MCT] wrote:
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.


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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Dec 9 '05 #4
archana wrote:
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.


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

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Dec 9 '05 #5
Hi

i am posting code of processing url asynchronously.

using System;
using System.Net;
using System.Threading;
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.GetDecoder();

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

// ClientGetAsync issues the async request.
class ClientGetAsync
{
public static ManualResetEvent allDone = new
ManualResetEvent(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.Create(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.BeginGetResponse(
new AsyncCallback(RespCallback), rs);

// Wait until the ManualResetEvent is set so that the application

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

Console.WriteLine(rs.RequestData.ToString());
}

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

private static void RespCallback(IAsyncResult 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.EndGetResponse(ar);

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

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

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

BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
// Get the RequestState object from AsyncResult.
RequestState rs = (RequestState)asyncResult.AsyncState;

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

// 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.BufferRead, 0, read, charBuffer,
0);

String str = new String(charBuffer, 0, len);

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

// Continue reading data until
// responseStream.EndRead returns -1.
IAsyncResult ar = responseStream.BeginRead(
rs.BufferRead, 0, BUFFER_SIZE,
new AsyncCallback(ReadCallBack), rs);
}
else
{
if(rs.RequestData.Length>0)
{
// Display data to the console.
string strContent;
strContent = rs.RequestData.ToString();
}
// Close down the response stream.
responseStream.Close();
// Set the ManualResetEvent 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.

Dec 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Leo | last post by:
Version 7 fixpack 9, aix 5.1 In our database we have a table that that has a dedicated bufferpool for the data, and another for indexes. Actually we have three of these combinations and they...
0
by: DotNetShadow | last post by:
Hi Guys I came across this article which deals with Performance Considerations for Making Web Service Calls from ASPX Pages:...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
11
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET applications with the use of static keywords"...
0
by: Demi | last post by:
http://msdn2.microsoft.com/en-us/library/21k58ta7.aspx I'm using that example as my starting point. The timeout it has though is not bulletproof. It will timeout if it doesn't connect within...
3
by: Maya Sam | last post by:
Hi all, I have the following code I created to do multiple websites data crawling using Asynchronous Thread calling it works fine however I'm confused when it comes to make the calling thread...
0
by: thomasabcd | last post by:
Hi, During the user's registration I wan't to geo-code an address using localsearchmaps.com/geo. For that purpose I use a webrequest like this: string url =...
0
by: archana | last post by:
Hi all, I am having application wherein i am processing some asynchronous webrequest. I have declared one timeout callback as specified in MSDN for implementing timeout for aysnchornous web...
0
by: APA | last post by:
I've seen the MS sample async web request pattern and I ask is it really async if it is using a ManualResetEvent and setting WaitOne()? The ManualResetEvent object is being declared as a static...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.