Let's suppose I have this function that can be called several times from the main thread. Every time this is called, I create a WebClient
object to download some data asynchronously.
My question... is this safe to do? Is the WebClient
object released after the event is called? I wouldn't like to keep allocating memory if it is not going to be freed automatically.
My application is for WP7 with Silverlight.
Thanks!
void DownloadData(string cURL)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted +=
new System.Net.DownloadStringCompletedEventHandler(
webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(cURL));
}
static void webClient_DownloadStringCompleted(object sender,
System.Net.DownloadStringCompletedEventArgs e)
{
...
}
The SilverLight version of WebClient
doesn't implement IDisposable
. You are doing it right - webClient
will be automatically garbage collected when the time comes.
Instead of manually disposing of WebClient you could just put it in a using block.
using (WebClient webClient = new WebClient())
{
// Your business in here...
}
I see 2 problems. First of all, webclient isnt disposed in all possible situations, secondly a reference to WebClient will be maintained since you never unsubscribe the event.
I think this comes close to it (still not perfect though, think about ThreadAborted):
void DownloadData(string cURL)
{
WebClient webClient = new WebClient();
try
{
webClient.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(cURL));
}
catch
{
webClient.Dispose();
throw;
}
}
static void webClient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
WebClient webClient = (WebClient)sender;
webClient.DownloadStringCompleted -= webClient_DownloadStringCompleted;
try
{
}
finally
{
webClient.Dispose();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With