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

--
Do or die..

Re: StreamWriter.close() hangs in multithreaded execution by Alvin

Alvin
Mon Feb 14 09:03:04 CST 2005

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" <jango@newsgroup.nospam> wrote in message
news:2E97DF71-056D-4B95-BC81-10AA68CA6D45@microsoft.com...
> 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
>
> --
> Do or die..



Re: StreamWriter.close() hangs in multithreaded execution by jango

jango
Mon Feb 14 13:37:08 CST 2005

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 [MVP]" wrote:

> 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" <jango@newsgroup.nospam> wrote in message
> news:2E97DF71-056D-4B95-BC81-10AA68CA6D45@microsoft.com...
> > 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
> >
> > --
> > Do or die..
>
>
>

Re: StreamWriter.close() hangs in multithreaded execution by jango

jango
Mon Feb 14 13:39:01 CST 2005

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

Jan


"Alvin Bruney [MVP]" wrote:

> 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" <jango@newsgroup.nospam> wrote in message
> news:2E97DF71-056D-4B95-BC81-10AA68CA6D45@microsoft.com...
> > 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
> >
> > --
> > Do or die..
>
>
>

Re: StreamWriter.close() hangs in multithreaded execution by Alvin

Alvin
Mon Feb 14 13:53:33 CST 2005

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_fssession=&BAS_vendor=0&bas_type=FADA&BAS_page=9&DAT_maxrecords=10&BAS_flag=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" <jango@newsgroup.nospam> wrote in message
news:7FA85C40-D7F7-4FE8-AB88-72FD673B2040@microsoft.com...
>I don't think I'm setting the useragent at all.
>
> Jan
>
>
> "Alvin Bruney [MVP]" wrote:
>
>> 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" <jango@newsgroup.nospam> wrote in message
>> news:2E97DF71-056D-4B95-BC81-10AA68CA6D45@microsoft.com...
>> > 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
>> >
>> > --
>> > Do or die..
>>
>>
>>



Re: StreamWriter.close() hangs in multithreaded execution by v-schang

v-schang
Mon Feb 14 23:30:05 CST 2005

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.)


Re: StreamWriter.close() hangs in multithreaded execution by jango

jango
Tue Feb 15 14:57:03 CST 2005

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

"Steven Cheng[MSFT]" wrote:

> 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.)
>
>

Re: StreamWriter.close() hangs in multithreaded execution by jango

jango
Tue Feb 15 15:03:04 CST 2005

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 [MVP]" wrote:

> 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_fssession=&BAS_vendor=0&bas_type=FADA&BAS_page=9&DAT_maxrecords=10&BAS_flag=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" <jango@newsgroup.nospam> wrote in message
> news:7FA85C40-D7F7-4FE8-AB88-72FD673B2040@microsoft.com...
> >I don't think I'm setting the useragent at all.
> >
> > Jan
> >
> >
> > "Alvin Bruney [MVP]" wrote:
> >
> >> 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" <jango@newsgroup.nospam> wrote in message
> >> news:2E97DF71-056D-4B95-BC81-10AA68CA6D45@microsoft.com...
> >> > 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
> >> >
> >> > --
> >> > Do or die..
> >>
> >>
> >>
>
>
>

Re: StreamWriter.close() hangs in multithreaded execution by v-schang

v-schang
Tue Feb 15 21:51:15 CST 2005

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.)



Re: StreamWriter.close() hangs in multithreaded execution by jango

jango
Wed Feb 16 10:25:04 CST 2005

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 :-(

"Steven Cheng[MSFT]" wrote:

> 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.)
>
>
>

Re: StreamWriter.close() hangs in multithreaded execution by Alvin

Alvin
Wed Feb 16 11:20:42 CST 2005

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
----------------------------------------------------------


"Jan" <jango@newsgroup.nospam> wrote in message
news:CE55817B-E1CA-4210-B74E-8F7B5C5B342A@microsoft.com...
> 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 :-(
>
> "Steven Cheng[MSFT]" wrote:
>
>> 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.)
>>
>>
>>