.NET:我需要一个参考保持Web客户端下载时异步? [英] .NET: Do I need to keep a reference to WebClient while downloading asynchronously?

查看:118
本文介绍了.NET:我需要一个参考保持Web客户端下载时异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的方法在一块的生产code:

I use the following method in a piece of production code:

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 Web客户端实例被垃圾引起C>事件被称为:退出我的 DownloadData()方法后,有到 Web客户端对象没有明显的引用,因此,可以振振有词地发生了。

I am now worried that a hard to reproduce bug might be caused by the WebClient instance being garbage collected before the DownloadDataCompleted event is called: after exiting my DownloadData() method, there are no obvious references to the WebClient object, so that could plausibly happen.

所以我的问题是:可以在此现实发生的呢?我无法重现的问题,所以可能有一些内部的事情发生了prevents被垃圾收集 Web客户端对象(如对象可能具有全球注册自己反对的地方在等待响应)。

So my question is: can this realistically happen? I can not reproduce the problem, so there might be some internal things happening that prevents the WebClient object from being garbage collected (e.g. the object might register itself with a global object somewhere while waiting for the response).

在code是.NET 2.0上运行,如果有什么差别。

The code is running on .NET 2.0 if that makes any difference.

推荐答案

没有,你的对象不会GC-ED中直到回调完成。据<一个href=\"http://stackoverflow.com/questions/421547/does-the-garbage-collector-destroy-temporarily-unreferenced-objects-during-async\">http://stackoverflow.com/questions/421547/does-the-garbage-collector-destroy-temporarily-unreferenced-objects-during-async, 异步API保持一个参考你的要求(线程池中异步IO操作都提出之内),因此垃圾回收,直到它完成它不会是。

No, your object won't be GC-ed until the callback completes. According to http://stackoverflow.com/questions/421547/does-the-garbage-collector-destroy-temporarily-unreferenced-objects-during-async, "the async API keeps a reference to your request (within the thread pool where async IO operations are lodged) and so it won't be garbage collected until it completes."

不过,您的code也做的东西并不需要:你并不需要分离事件处理程序,并不需要调用Dispose WebClient的。 (Dispose()方法实际上不受WebClient--实施佑能可在.NET Framework参考源看到这个<一个href=\"http://referencesource.microsoft.com/netframework.aspx\">http://referencesource.microsoft.com/netframework.aspx).

But, your code is also doing stuff it doesn't need to: you don't need to detach the event handler and don't need to call Dispose on the webclient. (Dispose() is actually not implemented by WebClient-- yiou can can see this in the .NET Framework reference source at http://referencesource.microsoft.com/netframework.aspx).

所以你实际上并不需要参考WebClient的实例,在回调。换句话说,下面的code将工作一样好,避免从内部委托引用外部的局部变量中的任何潜在问题(如上所述)。

So you don't actually need to refer to the webclient instance in your callback. In other words, the following code will work just as well, and avoid any potential issues (discussed above) of referencing external local variables from inside a delegate.

private void DownloadData(Uri uri)
{
    WebClient webClient = new WebClient();
    DownloadDataCompletedEventHandler eh = null;
    eh = delegate(object sender, DownloadDataCompletedEventArgs e)
    {
        OnDataDownloaded();
    };
    webClient.DownloadDataAsync(uri);
}

不管怎样,你可能要到别处寻找你的错误的来源。一个地方,我想看看是calls--你可能会运行内存的HTTP的结果,可能是遇到了服务器错误,等等。你可以看一下e.Error,看是否调用实际工作。

Anyway, you probably want to look elsewhere for the source of your bug. One place I'd look is at the results of the HTTP calls-- you may be running out of memory, may be running into server errors, etc. You can look at e.Error to see if the calls are actually working.

这篇关于.NET:我需要一个参考保持Web客户端下载时异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆