【发布时间】:2019-11-16 20:36:50
【问题描述】:
我想通过 WebClient 异步回调中的 ref 更新变量。
.DownloadStringAsync() (token) 中的第二个参数似乎不被 ref 接受,并且是 readonly (e.UserState) 所以我没有想法。
怎么做?
static void Main(string[] args)
{
string a = "AAA";
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted += Wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("http://someurl.to.json"), a);
}
Console.ReadKey();
Console.WriteLine(a);
}
private static void Wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
// do something with e.result ...;
// update the caller method's 'a' variable (by ref not possible as token) with "BBB"
}
【问题讨论】:
-
尝试将变量移动到全局范围内。
标签: c# .net asynchronous webclient