I used to have:
using (MyWebClient client = new MyWebClient(TimeoutInSeconds))
{
var res = client.DownloadData(par.Base_url);
//code that checks res
}
Now I have:
using (MyWebClient client = new MyWebClient(TimeoutInSeconds))
{
client.DownloadDataAsync(new Uri(par.Base_url));
client.DownloadDataCompleted += (sender, e) =>
{
//code that checks e.Result
}
}
Where MyWebClient is derived from WebClient.
Now I have lots of threads doing this and in the first case memory consumption wasn’t an issue while in the second one I see steady rise in memory until I get OutOfMemoryException.
I profiled and it seems that WebClient is the culprit, not being disposed and downloaded data is kept. But why? What’s the difference between two cases? Perhaps e.Result needs to be somehow disposed of?
Your first case limits the number of concurrent downloads to the number of threads. Your second case has no limit on the number of concurrent downloads.