Take the 2-minute tour ×

This method in my .aspx file...

   private static string GetPageAsString(string address)
            TimeSpan timeout = new TimeSpan(1000000);  // 1 second=10,000,000          
            DateTime tryToGetAnswerUntil = DateTime.Now.Add(timeout);
            string returnXML = "";
            while (string.IsNullOrEmpty(returnXML) && DateTime.Now < tryToGetAnswerUntil )
                returnXML = AsyncWebRequester.GetResult(address);                
            return returnXML;

accesses methods in this class...

public class AsyncWebRequester
        private static Dictionary<string, string> retrievedxml = new Dictionary<string, string>();

        public static void StartRequest(string uri)
                InitiateAsyncRequestDelegate mydelegate = new InitiateAsyncRequestDelegate(InitiateAsyncRequest);
                mydelegate.BeginInvoke(uri, null, null);
            catch {}

        private delegate void InitiateAsyncRequestDelegate(string uri); 

        private static void InitiateAsyncRequest(string uri) 
            WebRequest request;
                request = WebRequest.Create(uri);              

                using (WebResponse response = request.GetResponse())
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string result = reader.ReadToEnd();
                    retrievedxml.Add(uri, result);
            catch { }
            finally { request = null; }

        public static string GetResult(string uri)
            string xml = "";
            if (retrievedxml.ContainsKey(uri))
                xml = retrievedxml[uri];
            return xml;              


Basically this means: fire-and-forget some threads to ask for xml from a website. If I get xml back from the site, fine; otherwise I just forget about the thread. In any case, actually, I just forget about the thread (as far as I know).

The question is: Is that ok, or should I do something to actively terminate the threads (EndInvoke()?), or does the GarbageCollector handle it for me, so I dont risk starving the memory or bringing the server to it's knees in some other way. In other words: Is my code bullet-proof?

share|improve this question
BTW, TimeSpan.FromSeconds(1), or DateTime.Now.AddSeconds(1) –  SLaks Feb 10 '12 at 18:43
Your finally block is completely useless. –  SLaks Feb 10 '12 at 18:44
What exactly are you trying to do? GetPageAsString() is still just going to block. You might as well do everything synchronously. –  scottm Feb 10 '12 at 18:53
Your waiting code is extremely inefficient. You should use a timed WaitOne() on a WaitHandle. –  SLaks Feb 10 '12 at 18:56
add comment

1 Answer

You do need to call EndInvoke().

In your case, you should just call ThreadPool.QueueUserWorkItem instead; it's simpler.

share|improve this answer
Thanks for all the replies. Let me reply to them from the top: –  Peter Feb 11 '12 at 14:04
* FromSeconds / addSeconds only takes integers; I need to set timeout to fractions of seconds. * why is my finally-block useless? –  Peter Feb 11 '12 at 14:23
* What I am trying to do: Initially, I used WebResponse synchronously, setting WebResponse.TimeOut to ½ second. Problem: TimeOut has no effect if the server from which I am trying to get a response, is offline, which means I could get to wait 15 seconds. So basically I just neeedeed some way to control the timeout, and this is what I came up with (my multithreading experience not being very long). I can live with this implementation, my main concern being the one I mentioned about memorystarvation. * Does calling EndInvoke kill the threads? –  Peter Feb 11 '12 at 14:24
Maybe I should rephrase my question: I need to make a number of requests to "worldweatheronline.com?city=...";, which returns time and weather as xml (which I cache on my server for 15 minutes). Sometimes worldweatheronline is down, so I don't want to make my application hang for 10 seconds or more pr. request, which it will if I execute every request synchronously, one after the other. –  Peter Feb 11 '12 at 14:24
1) Wrong; both AddSeconds and FromSeconds take doubles. –  SLaks Feb 12 '12 at 4:00
show 8 more comments

Your Answer


