假設我有這個函數可以在主線程中多次調用。每次調用時,我都會創建一個WebClient
對象來異步下載某些數據。在訂閱了異步事件後,對象是否會自動處理?
我的問題......這是安全的嗎?事件被調用後,WebClient
對象是否被釋放?如果不會自動釋放內存,我不想繼續分配內存。
我的應用程序是WP7與Silverlight。
謝謝!所以沒有什麼特別需要做,允許適當的垃圾收集
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)
{
...
}
不清楚活動對象是否持有對WebClient對象的引用。 DownloadStringAsync可能會設置一些東西來做到這一點,但我會更擔心它太早收集垃圾,而不是太晚! :) – 2011-05-19 18:51:18