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