.NET: Do I need to keep a reference to WebClient while downloading asynchronously?

后端 未结 5 1006
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 21:54

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

private void DownloadData(Uri uri)
{
    WebClient webClient = new WebClient();
    DownloadDataCom         


        
5条回答
  •  Happy的楠姐
    2020-12-14 22:35

    You can try debugging the application with Debugging Tools for Windows - it allows you to see what exactly is keeping references to a specific object (with the appropriate plug-in). Very useful for such cases.

    I do not know the answer to your question, though. One possibility is that for the duration of the operation, WebClient makes itself one of the "root" objects, which are never garbage collected (a .NET application generally has around 5 to 10 such objects, which are the roots of several reference trees used by the application). This is pure speculation, though.

提交回复
热议问题