WebRequest hangs when run in own thread

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

Guest

Hi

I'm experiencing a strange problem when trying to issue a WebRequest from a
thread other than the main thread:

using System;
using System.Threading;
using System.Net;
using System.IO;

namespace HangProblem
{
class Program
{
static void Main(string[] args)
{
new Thread(delegate()
{
try
{
Console.WriteLine("Getting google index page");
WebRequest req = WebRequest.Create("http://www.google.com");
TextReader r = new
StreamReader(req.GetResponse().GetResponseStream());
Console.WriteLine(r.ReadToEnd());
r.Close();
}
catch (Exception e) { Console.WriteLine("Failed: {0}", e.Message); }
}).Start();

Console.ReadLine();
}
}
}

When running the program, the thread simply hangs (i.e. nothing happens). If
I leave out the thread part and just issue the WebRequest from main all works
fine. I have tried setting both STAThread and MTAThread attributes on main
and tried changing thread priorities - it makes no difference. I experience
the same problem in other situations, especially when using COM interop.

I'm running VS2005/.NET framework 2.0 beta1.


/anders
 
Perhaps it is a bug with anonymous methods. You should probablyh check
out http://lab.msdn.microsoft.com/ProductFeedback/ and submit a sample
project there along with a description of what happens. It also would
be helpful if you were able to isolate exactly what causes it to break.
For example, does it work fine if you run the thread as a normal method
(not an anonymous method) and if so provide a sample code.
 
It is not an anonymous method problem. I used an anonymous method in
the example to keep it as short/simple as possible, but declaring the thread
entry function as a "normal" method gives me the same result.
 
This sample code does not compile. If you care to fix it, i or someone else
can take a look.

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
_________________________
 
With a standard installation of Whidbey beta1 (v8.0.40607) the sample
compiles like a charm.


Alvin Bruney said:
This sample code does not compile. If you care to fix it, i or someone else
can take a look.

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
_________________________


Anders M. Mikkelsen said:
Hi

I'm experiencing a strange problem when trying to issue a WebRequest from
a
thread other than the main thread:

using System;
using System.Threading;
using System.Net;
using System.IO;

namespace HangProblem
{
class Program
{
static void Main(string[] args)
{
new Thread(delegate()
{
try
{
Console.WriteLine("Getting google index page");
WebRequest req = WebRequest.Create("http://www.google.com");
TextReader r = new
StreamReader(req.GetResponse().GetResponseStream());
Console.WriteLine(r.ReadToEnd());
r.Close();
}
catch (Exception e) { Console.WriteLine("Failed: {0}",
e.Message); }
}).Start();

Console.ReadLine();
}
}
}

When running the program, the thread simply hangs (i.e. nothing happens).
If
I leave out the thread part and just issue the WebRequest from main all
works
fine. I have tried setting both STAThread and MTAThread attributes on main
and tried changing thread priorities - it makes no difference. I
experience
the same problem in other situations, especially when using COM interop.

I'm running VS2005/.NET framework 2.0 beta1.


/anders
 
I think Alvin might have accidentally compiled it on VS.NET 2003
without realizing that you were using anonymous methods which are not
supported in 1.1
 
yup. sorry, i'm not that bright.

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
_________________________
 
I forgot to mention that the thing only hangs when run in the debugger.
Outside
the debugger there are no problems.


/anders
 
Back
Top