假设我有一个可以从主线程多次调用的函数。每次调用它时,我都会创建一个 WebClient
对象来异步下载一些数据。
我的问题...这样做安全吗? WebClient
对象是否在事件调用后释放?如果内存不会自动释放,我不想继续分配内存。
我的应用程序适用于带有 Silverlight 的 WP7。
谢谢!
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)
{
...
}
最佳答案
SilverLight version of WebClient
没有实现 IDisposable
。你做得对 - webClient
会在时机成熟时自动进行垃圾回收。
关于c# - 引发订阅的异步事件后,对象会自动处理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063079/