さまざまな URL から JSON ファイルにアクセスしようとしています。他の何かを実行する前に、両方のファイルが完全にダウンロードされていることを確認するにはどうすればよいですか?
私のコードは次のようになります。
WebClient Url1 = new WebClient();
Url1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url1_DownloadStringCompleted);
Url1.DownloadStringAsync(new Uri("http://example.com"));
WebClient Url2 = new WebClient();
Url2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url2_DownloadStringCompleted);
Url2.DownloadStringAsync(new Uri("http://anotherexample.com"));
DoSomethingWithJson();
void Url1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
json1 = JObject.Parse(e.Result);
}
void Url2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
json2 = JObject.Parse(e.Result);
}
現在、json1 と json2 は、DoSomethingWithJson() 内の MessageBox に表示しようとすると常に null 値を返します。これは、完全にダウンロードされていないことが原因である可能性があると想定しています。