StreamWriter.close() hangs in multithreaded execution

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a program accessing a HTTP service to get som XML data.

The code receiveing the data looks something like this:
stream = new StreamWriter(httpRequest.GetRequestStream(),Encoding.UTF8);
stream.Write(body);
stream.Close();

The httpRequest is a HttpWebRequest object.

This works fine from a single thread execution, but when I'm accessing this
method on its own thread, the Close() method seems to hang.

Any ideas as to what I'm doing wrong?

Jan
 
It shouldn't. Are you setting the useragent correctly? How do you know it is
hanging? Are there any exceptions?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 
This is a program that collects data from several different services. I was
hoping to make it more efficient by enabling different threads to do the
collection in parallell. The service answers promptly when called from the
main thread, but when called from it's own thread it just stops on the close
method of the StreamWriter. I've incorporated a timeout to abort any
collection thread that uses more than a specified amount of time, and this is
what finally throws a ThreadAbort exception. While the close call is
"waiting" the cpu usage for the aspnet process is hovering around 90% cpu
usage. Extending the timeout period doesn't change anything. I'm actually
seeing the same problem with another collection function that interfaces with
a web service..

Jan


Alvin Bruney said:
It shouldn't. Are you setting the useragent correctly? How do you know it is
hanging? Are there any exceptions?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Jan said:
I've got a program accessing a HTTP service to get som XML data.

The code receiveing the data looks something like this:
stream = new StreamWriter(httpRequest.GetRequestStream(),Encoding.UTF8);
stream.Write(body);
stream.Close();

The httpRequest is a HttpWebRequest object.

This works fine from a single thread execution, but when I'm accessing
this
method on its own thread, the Close() method seems to hang.

Any ideas as to what I'm doing wrong?

Jan
 
I don't think I'm setting the useragent at all.

Jan


Alvin Bruney said:
It shouldn't. Are you setting the useragent correctly? How do you know it is
hanging? Are there any exceptions?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Jan said:
I've got a program accessing a HTTP service to get som XML data.

The code receiveing the data looks something like this:
stream = new StreamWriter(httpRequest.GetRequestStream(),Encoding.UTF8);
stream.Write(body);
stream.Close();

The httpRequest is a HttpWebRequest object.

This works fine from a single thread execution, but when I'm accessing
this
method on its own thread, the Close() method seems to hang.

Any ideas as to what I'm doing wrong?

Jan
 
here is my threaded code that i use. see if you are missing anything.

strNumber =
System.Text.RegularExpressions.Regex.Replace(strNumber,"\\D",string.Empty);

// Create a new 'Uri' object with the specified string.

Uri myUri =new
Uri("http://adp.infousa.com/fs?BAS_fsses...=2&BAS_flag2=1&SCH_origdb=FADP&sch_fullphone="
+ strNumber);

// Creates an System.Net.HttpWebRequest with the specified URL.

System.Net.HttpWebRequest myHttpWebRequest =
(System.Net.HttpWebRequest)WebRequest.Create(myUri);

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; Q312461; .NET CLR 1.0.3705)";

HttpWebResponse res = (HttpWebResponse)myHttpWebRequest.GetResponse();

StreamReader sr = new StreamReader(res.GetResponseStream(),
System.Text.Encoding.UTF8);

string pageContent = sr.ReadToEnd();

res.Close();

sr.Close();



--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Jan said:
I don't think I'm setting the useragent at all.

Jan


Alvin Bruney said:
It shouldn't. Are you setting the useragent correctly? How do you know it
is
hanging? Are there any exceptions?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Jan said:
I've got a program accessing a HTTP service to get som XML data.

The code receiveing the data looks something like this:
stream = new
StreamWriter(httpRequest.GetRequestStream(),Encoding.UTF8);
stream.Write(body);
stream.Close();

The httpRequest is a HttpWebRequest object.

This works fine from a single thread execution, but when I'm accessing
this
method on its own thread, the Close() method seems to hang.

Any ideas as to what I'm doing wrong?

Jan
 
Thanks for Alvin's detail inputs,

Jan,

I think you can also try do some simple tests by posting some general
simple datas through HTTPwebRequest in multi-threading condition to see
whether the problem also occurs. Based on my experience, this is also
possible caused by the default limited network connections count in
..net's System.Net setting. The default limitation is 2. We can adjust this
value in the

<connectionMangement> element in config file:
#Configuring Internet Applications
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconconfiguringinterne
tapplications.asp?frame=true

or using the ServicePointManger class's "DefaultConnectionLimit" property.

HTH. Thanks.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
After setting the connectionManagement setting I do get an IOException from
time to time. I've facilitated the code so that I can run the specific code
from the main thread and its own thred, and It always works from the main
thread.

The exception thrown is "Unable to write to the transport connection"

I'll try to set the UserAgent as advised, but can't see why this should make
a difference..

Jan
 
I see from youre code that you're closing the httpRequest object before
you're closing the StreamWriter.
How come? Could this result in the problems I'm seeing? (I'll check :-)

Jan


Alvin Bruney said:
here is my threaded code that i use. see if you are missing anything.

strNumber =
System.Text.RegularExpressions.Regex.Replace(strNumber,"\\D",string.Empty);

// Create a new 'Uri' object with the specified string.

Uri myUri =new
Uri("http://adp.infousa.com/fs?BAS_fsses...=2&BAS_flag2=1&SCH_origdb=FADP&sch_fullphone="
+ strNumber);

// Creates an System.Net.HttpWebRequest with the specified URL.

System.Net.HttpWebRequest myHttpWebRequest =
(System.Net.HttpWebRequest)WebRequest.Create(myUri);

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; Q312461; .NET CLR 1.0.3705)";

HttpWebResponse res = (HttpWebResponse)myHttpWebRequest.GetResponse();

StreamReader sr = new StreamReader(res.GetResponseStream(),
System.Text.Encoding.UTF8);

string pageContent = sr.ReadToEnd();

res.Close();

sr.Close();



--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Jan said:
I don't think I'm setting the useragent at all.

Jan


Alvin Bruney said:
It shouldn't. Are you setting the useragent correctly? How do you know it
is
hanging? Are there any exceptions?

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


I've got a program accessing a HTTP service to get som XML data.

The code receiveing the data looks something like this:
stream = new
StreamWriter(httpRequest.GetRequestStream(),Encoding.UTF8);
stream.Write(body);
stream.Close();

The httpRequest is a HttpWebRequest object.

This works fine from a single thread execution, but when I'm accessing
this
method on its own thread, the Close() method seems to hang.

Any ideas as to what I'm doing wrong?

Jan
 
Thanks for your followup Jan,

From the exception you mentioned:
============
The exception thrown is "Unable to write to the transport connection"
==============

seems the http connection is somehow locked? Would you have a further check
to see whether there is
any stream object that you haven't closed? From my local tests, i can
correctly post multi files in multi concurrent threads without any
problems(Except that the server return 500 error if I send posts too
frequent).

Thanks & Regards,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
It seems the problem was related to my development environment. The
component worked perfectly in a production environment. Apparently the
development environment, and especially the debugger, wrecked havoc in the
network communication.

At least the code is up and running although many hourc have been lost
debugging :-(
 
chuck it up to experience, then it won't be wasted.

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 
Back
Top