gpt4 book ai didi

c# - 引发订阅的异步事件后,对象会自动处理吗?

转载 作者:太空狗 更新时间:2023-10-29 20:25:30 25 4
gpt4 key购买 nike

假设我有一个可以从主线程多次调用的函数。每次调用它时,我都会创建一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com