我的 uriRead 方法似乎在异步下载完成之前返回,导致该方法返回“”。如果我把 Thread.Sleep(5000) 放在“//在这里等待?” 但是,它会完成。
我怎样才能让这个函数等待字符串下载完成并在不输入静态延迟的情况下立即返回?
public string uriRead(string uri)
{
string result = "";
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AsyncReadCompleted);
client.DownloadStringAsync(new Uri(uri));
// Wait here?
return result = downloadedAsyncText;
}
public void AsyncReadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Console.WriteLine("Event Called");
downloadedAsyncText = e.Result.ToString();
Console.WriteLine(e.Result);
}