I recently wrote a tool to auto-post information to Twitter every 3 minutes. However I hit a problem that took me a week to figure out.
Each time the program would work fine for the first two or so posts, then shortly after it would start showing a Timeout instead of posting. Wireshark would show no attempt to contact Twitter!
After lots of searching on google and trying to find advice. Lots of which were set the timeout, set the servicepointmanager timeout, ServicePointManager.DefaultConnectionLimit etc..
A lot of places suggested using GC.Collect (Garbage Collection) after each post. This also didn’t help.
What finally did solve the problem was simple.. Changing my code from this:
dim response As WebResponse = request.GetResponse() Return response.ToString response.Close() response = nothing GC.Collect()
Using response As WebResponse = request.GetResponse() Return response.ToString response.Close() End Using GC.Collect()
Even though I was closing response and setting it to nothing somehow something about the “Using” clause does better connection tidying or thread control.