【发布时间】:2010-11-02 01:29:02
【问题描述】:
我在一段生产代码中使用如下方法:
private void DownloadData(Uri uri)
{
WebClient webClient = new WebClient();
DownloadDataCompletedEventHandler eh = null;
eh = delegate(object sender, DownloadDataCompletedEventArgs e)
{
webClient.DownloadDataCompleted -= eh;
((IDisposable) webClient).Dispose();
OnDataDownloaded();
};
webClient.DownloadDataCompleted += eh;
webClient.DownloadDataAsync(uri);
}
我现在担心在调用DownloadDataCompleted 事件之前被垃圾收集的WebClient 实例可能会导致难以重现的错误:退出我的DownloadData() 方法后,没有明显引用@ 987654325@ 对象,所以这可能会发生。
所以我的问题是:这真的会发生吗?我无法重现该问题,因此可能发生了一些内部问题,阻止了 WebClient 对象被垃圾收集(例如,在等待响应时,该对象可能会在某处向全局对象注册自身)。
如果这有什么不同的话,代码在 .NET 2.0 上运行。
【问题讨论】:
标签: c# .net asynchronous garbage-collection webclient