473,503 Members | 12,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreading webresponse

RS
I have written a utility which currently downloads xml data from a high
number (40+) of web based sources in a synchronous manner.

When attempting to spawn threads off to speed things up I run into
major problems due to blocking and number of threads available in the
thread pool.

Despite trying various methods (invoking delegates, counting threads in
order to try manage the threadpool, etc) none of these have worked as
hoped.

Basically all my code is doing is calling the following function:

internal myclass DeserializeMyClass(string url)
{
XmlSerializer serializer = new XmlSerializer(typeof(myclass));
myclass t = new myclass();

try
{
WebRequest wReq = WebRequest.Create(url);
WebResponse wRes = wReq.GetResponse(); // this is the culprit!!!
StreamReader strm = new StreamReader(wRes.GetResponseStream());
XmlTextReader xmlReader = new XmlTextReader(strm);
t = (myclass) serializer.Deserialize(xmlReader);
}
catch
{
// todo: log error
}

return t;
}

Can anyone point to how I could successfully do this in a
multi-threaded manner?

Nov 17 '05 #1
6 2494
Are you sure your bandwidth is not the bottleneck? You can spawn as
many threads as you like, but if bandwidth is the limiting factor, then
those threads will actually slow things down.

How did you attempt to spawn new threads? If the system threadpool
might not be ideal for your needs, you can try a custom one.

Regards
Senthil

Nov 17 '05 #2
RS

S. Senthil Kumar wrote:
Are you sure your bandwidth is not the bottleneck? You can spawn as
many threads as you like, but if bandwidth is the limiting factor, then
those threads will actually slow things down.

How did you attempt to spawn new threads? If the system threadpool
might not be ideal for your needs, you can try a custom one.

Regards
Senthil


The actual error messages (when I received them) related that there
where not enough threads in the threadpool.

I tried creating a delegate for the function and calling via
begininvoke, as well as various other methods.

In most cases the webresponse line seemed to be the problem, as the
debugger only stepped here before the next thread spawned. None of the
functions then responded with a valid class as the threads simply timed
out.

Nov 17 '05 #3
RS <rs********@delphi-dolphin.com> wrote:
The actual error messages (when I received them) related that there
where not enough threads in the threadpool.


In that case (and anyway, frankly) I'd avoid using the system
threadpool - use either new threads, or (preferrably, IMO) a custom
threadpool. That way you don't end up with hard-to-debug problems due
to other methods using the system threadpool unexpectedly, and you have
much more control over how many threads it can use etc.

You may, however, want to consider using asynchronous IO instead. This
would be more complicated but I believe it would avoid creating quite
as many new threads. (I'm not entirely sure of the implementation.)

If you want a custom threadpool, there are plenty of free ones around.
The one I've written is at
http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
RS...

At what point are you seeing the blocking? After 2,3,4 threads have been
spawned?

It sounds like an http ConnectionLimit problem meaning you can only be
downloading 2 web requests/responses at a given time. However, this is
configurable via the ServicePoint class using the ConnectionLimit property.
Try setting this property to the number of threads you are going to spawn
and see if this fixes.

Yosh
"RS" <rs********@delphi-dolphin.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have written a utility which currently downloads xml data from a high
number (40+) of web based sources in a synchronous manner.

When attempting to spawn threads off to speed things up I run into
major problems due to blocking and number of threads available in the
thread pool.

Despite trying various methods (invoking delegates, counting threads in
order to try manage the threadpool, etc) none of these have worked as
hoped.

Basically all my code is doing is calling the following function:

internal myclass DeserializeMyClass(string url)
{
XmlSerializer serializer = new XmlSerializer(typeof(myclass));
myclass t = new myclass();

try
{
WebRequest wReq = WebRequest.Create(url);
WebResponse wRes = wReq.GetResponse(); // this is the culprit!!!
StreamReader strm = new StreamReader(wRes.GetResponseStream());
XmlTextReader xmlReader = new XmlTextReader(strm);
t = (myclass) serializer.Deserialize(xmlReader);
}
catch
{
// todo: log error
}

return t;
}

Can anyone point to how I could successfully do this in a
multi-threaded manner?

Nov 17 '05 #5
RS wrote:
I have written a utility which currently downloads xml data from a
high number (40+) of web based sources in a synchronous manner.

When attempting to spawn threads off to speed things up I run into
major problems due to blocking and number of threads available in the
thread pool.

Despite trying various methods (invoking delegates, counting threads
in order to try manage the threadpool, etc) none of these have worked
as hoped.

Basically all my code is doing is calling the following function:

internal myclass DeserializeMyClass(string url)
{
XmlSerializer serializer = new XmlSerializer(typeof(myclass));
myclass t = new myclass();

try
{
WebRequest wReq = WebRequest.Create(url);
WebResponse wRes = wReq.GetResponse(); // this is the culprit!!!
StreamReader strm = new StreamReader(wRes.GetResponseStream());
XmlTextReader xmlReader = new XmlTextReader(strm);
t = (myclass) serializer.Deserialize(xmlReader);
}
catch
{
// todo: log error
}

return t;
}

Can anyone point to how I could successfully do this in a
multi-threaded manner?


I'd start by fixing the rather obvious resource leaks here: You never
close any request or response object.

Also note that the number of simultaneous TCP connections to a single
host is limited by ServicePoint.ConnectionLimit.

Cheers.
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #6
RS

In addition to Joerg.

Http 1.1

http://support.microsoft.com/default...b;en-us;183110

I hope this helps,

Cor
Nov 17 '05 #7

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

Similar topics

16
8469
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
0
2751
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
2
9684
by: gizmo | last post by:
Hi, I'm using the following code to request the html source from the quoted site. ...... string url = "http://www1.soccerstand.com/"; WebRequest webRequest = WebRequest.Create(url);...
2
1575
by: trialproduct2004 | last post by:
Hi all. i am having problem at a time of validatiang url. I am using webrequest and webresponse class to get content of particular url. My problem is after using webrequest and webresponse class...
2
3377
by: Jeff G. | last post by:
Hello everyone, I have read through the newsgroups (thank God for 'em!) extensively looking for an example of how to pass a file (PDF) from a webresponse stream down to a web client. Here's the...
1
8169
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie...
0
1487
by: archana | last post by:
Hi all, I am facing very strange problem while using webrequest. What i am doing is i have one url which i want to validate. so i am using webrequest and webresponse. My code is as below:-...
3
2326
by: KyleUbenk | last post by:
Well I have a multi-threaded program which downloads URL sources from the web. Im doing this multiple times at once. The only problem I see is that sometimes it takes a while to download the...
4
2120
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.