1

I have a problem. This part of code returns after ~1min System.OutOfMemoryException error. But I can't find what exacly causes this error. I would be glad if somebody will tell me what is the reason of this issue, and how to resolve it.

Part of my thread:

    public void Run(object client)
    {
        TextBox tbServerResult = (client as Client).Controls.Find("tbServerResult", true).SingleOrDefault() as TextBox;
        Client tt = new Client();
        for (int i = 0; i < 1; i--)
        {
            Thread.Sleep(10000);

            string result = tt.SendGet("xyz" + tt.getToken() + "");
            tbServerResult.AppendText(result);
        }
    }

SendGet method:

    public string SendGet(string url)
    {

        string webpageContent = "";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    webpageContent = reader.ReadToEnd();
                }
                //tbServerResult.AppendText("\r\n" + "Server code:" + webResponse.StatusCode + " Server status description:" + webResponse.StatusDescription);
                webpageContent = "Server code:" + webResponse.StatusCode + " Server status description:" + webResponse.StatusDescription;
            }


        }
        catch (Exception ex)
        {
            webpageContent = ex.Message;
        }

        return webpageContent;
    }
6
  • Could you point which line throws an exception? You can also show a URL that you are requesting Commented Dec 23, 2013 at 10:31
  • Other than the reader.ReadToEnd() I don't see any likely candidates for an OutOfMemoryException either, but even then that would mean some pretty fast downloading if it runs out after 1 minute... Commented Dec 23, 2013 at 10:39
  • 1
    Looks suspiciously like GUI operations in non-GUI thread. Commented Dec 23, 2013 at 10:40
  • @MartinJames That should result in a different type of exception, although I agree the AppendText on a background thread should fail. So probably the OutOfMemoryException occurs before it actually tries to access the control. Commented Dec 23, 2013 at 10:43
  • Also, how many of these threads are you creating? Also, what's with the sleep call anyway - just debugging? Commented Dec 23, 2013 at 10:59

1 Answer 1

4

You have line with for (int i = 0; i < 1; i--)

this is infinite loop.

Sign up to request clarification or add additional context in comments.

2 Comments

While you are right I would have though that the Thread.Sleep means it shouldn't run enough to get an out of memory exception in a minute...
There is an "infinite" loop in the code, but if the error occurs after about 1 minute, that cannot be the problem. Only 1 iteration is executed per 10 seconds at maximum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.