웹에서 두 개의 JSON 파일을 다운로드하고 난 후 두 페이지를로드 할 수 있지만 이전에는 허용하지 않습니다. 그러나 페이지를로드하기 위해 설정해야하는 ManualResetEvent
은 절대로 "실행되지 않습니다." 비록 내가 그것이 도착한다는 것을 알지만, WaitOne
결코 반환하지 않는다. Downloads (다운로드) 시작ManualResetEvent wait는 설정 한 후에 해제되지 않습니다.
방법 :
private void Application_Launching(object sender, LaunchingEventArgs e)
{
PhoneApplicationService.Current.State["doneList"] = new List<int>();
PhoneApplicationService.Current.State["manualResetEvent"] = new ManualResetEvent(false);
Helpers.DownloadAndStoreJsonObject<ArticleList>("http://arkad.tlth.se/api/get_posts/", "articleList");
Helpers.DownloadAndStoreJsonObject<CompanyList>("http://arkad.tlth.se/api/get_posts/?postType=webbkatalog", "catalog");
}
다운로드 방법, 즉 (이 경우 생성자)
public SenastePage()
{
InitializeComponent();
if ((PhoneApplicationService.Current.State["doneList"] as List<int>).Count < 2)
{
(PhoneApplicationService.Current.State["manualResetEvent"] as ManualResetEvent).WaitOne();
}
SenasteArticleList.ItemsSource = (PhoneApplicationService.Current.State["articleList"] as ArticleList).posts;
}
내가 만약 ManualResetEvent
public static void DownloadAndStoreJsonObject<T>(string url, string objName)
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Result))
{
var obj = ProcessJson<T>(e.Result);
PhoneApplicationService.Current.State[objName] = obj;
var doneList = PhoneApplicationService.Current.State["doneList"] as List<int>;
doneList.Add(0);
if (doneList.Count == 2) // Two items loaded
{
(PhoneApplicationService.Current.State["manualResetEvent"] as ManualResetEvent).Set(); // Signal that it's done
}
}
};
webClient.DownloadStringAsync(new Uri(url));
}
필요한 대기 방법을 설정합니다 해당 생성자에 액세스하기 전에 기다리십시오. if 문을 쉽게 전달합니다. WaitOne
에 잡히지는 않겠지 만, 즉시 호출하면 나는 막히고 반환하지 않습니다 ...
아이디어가 있으십니까?
교착 상태입니다. WebClient의'DownloadStringCompleted' 이벤트는'WaitOne'으로 블로킹하는 스레드와 동일한 UI 스레드에서 실행됩니다. 어쨌든 UI 스레드를 막론하고 차단하지 않아야합니다. 데이터를 기다리는 동안로드 애니메이션을 표시하는 것이 좋습니다. –
디버거가이 정지 상태에있는 동안 디버거에 놓을 수 있으며 doneList.Count 및 manualResetEvent 변수를 검사하여 해당 시점에 설정된 값을 확인할 수 있습니까? –
@KooKiz : 스플래시 화면을 표시하지 않거나, 어떤 시점에서 두 다운로드가 모두 완료된 것을 알고 있어야합니다. ManualResetEvent가 필요하지 않습니까? – SamiHuutoniemi