0

My desktop .NET 3.5 application uses WebRequest.BeginGetResponse to issue a request which is used to implement long polling. This request typically takes 10 seconds or more to process. The trouble is BeginGetResponse performs the waiting for the request to complete on the thread pool, which then seems to be starving. Is there some way how to specify a custom thread or a custom thread for BeginGetResponse to use?

As an alternative I could also use my own thread performing synchronous request using WebRequest.GetResponse (hopefully this is really synchronous in .NET 3.5, it was not in .NET 1.0), but then I have a problem I am unable to terminate the request prematurely when I need to quit the application, as I see no clean way how to abort a synchronous request.

Suma
  • 33,181
  • 16
  • 123
  • 191
  • It seems I was wrong in diagnosing the problem. The problem was not (or not only) the thread pool starvation, but rather [ServicePointManager.DefaultConnectionLimit](http://stackoverflow.com/a/7627892/16673). – Suma Aug 08 '13 at 14:41

1 Answers1

1

I don't know if you're using WPF or Windows forms.. Here's a WPF Example.

        WebRequest w = HttpWebRequest.Create("http://www.google.com");

        Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

        Thread thread = new Thread(new ThreadStart(() =>
            {
                WebResponse response = w.GetResponse();
                dispatcher.BeginInvoke(new Action(() =>
                {

                    // Handle response on the dispatcher thread.

                }), null);
            }));

        thread.IsBackground = true;
        thread.Start();

Notice the IsBackground = true. So your application will terminate it on exit. It's also possible to put HttpWebRequest.Create inside the thread method.

Windows.Form equivalent

        WebRequest w = HttpWebRequest.Create("http://www.google.com");

        Thread thread = new Thread(new ThreadStart(() =>
            {
                WebResponse response = w.GetResponse();
                this.BeginInvoke(new Action(() =>
                {

                    // Handle response on the dispatcher thread.

                }), null);
            }));

        thread.IsBackground = true;
        thread.Start();

Where this is a Form/Control

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • I am using WinForms. I am not familiar with WPF, however your code does not seem to be WPF specific, or am I missing something? Both Thread and Dispatcher seem to be available without WPF. Looks promising, will give it a try. – Suma Aug 08 '13 at 13:35
  • The dispatcher isn't used in Windows.Forms, so I think you could invoke on the dispatcher and on first call a dispatcher object will be created, but it won't be invoked because that dispatcher isn't running. (like a message loop) – Jeroen van Langen Aug 08 '13 at 13:36
  • Actually this is in a background thead, with no need to any UI reponse. It therefore shows in my case I can simply use a synchronouse WebRequest.GetResponse and mark my background thread with IsBackground = true, and I then do not have to worry about it not terminating soon enough. – Suma Aug 08 '13 at 13:40