2012-05-09 10 views
0

私は、いくつかのデバイスとWebサイト(上、下など)の状態を示すSilverlightダッシュボードを作成しています。 WebClientクラスを使用してWebサイトに接続し、WebClientクラスが稼動しているかどうかを確認しようとしています。しかし、DownloadStringCompletedイベントハンドラは決して起動しません。これはthis postと非常によく似た問題です。WebclientのDownloadStringCompletedイベントハンドラが起動しない

public void LoadPortalStatus(Action<IEnumerable<ChartModel>> success, Action<Exception> fail) 
{ 
    List<NetworkPortalStatusModel> pingedItems = new List<NetworkPortalStatusModel>(); 

    // Add the status for the portal 
    BitmapImage bi = IsPortalActive() 
      ? (new BitmapImage(new Uri("led_green_black-100x100.png", UriKind.Relative))) 
      : (new BitmapImage(new Uri("led_red_black-100x100.png", UriKind.Relative))); 

    NetworkPortalStatusModel nsm = new NetworkPortalStatusModel 
    { 
     Unit = "Portal", 
     StatusIndicator = new Image { Width = 100, Height = 100, Source = bi } 
    }; 

    pingedItems.Add(nsm); 

    // Send back to the UI thread 
    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(_delagateSuccess, new object[] { pingedItems }); 
} 

private bool IsPortalActive() 
{ 
    bool IsActive = false; 

    WebClient wc = new WebClient(); 
    wc.DownloadStringCompleted += (s, e) => 
     { 
      if (e.Cancelled) 
      { 
       _delagateFail(new Exception("WebClient page download cancelled")); 
      } 
      else if (e.Error != null) 
      { 
       _delagateFail(e.Error); 
      } 
      else 
      { 
       _portalHtmlResponse = e.Result; 
       if (_portalHtmlResponse.Contains("Somerville, Ma")) 
       { 
        IsActive = true; 
       } 
      } 
     }; 
    wc.DownloadStringAsync(new Uri("https://portal.nbic.com/monitor.aspx")); 

    return IsActive; 
} 

ここに問題が表示されますか?

答えて

0

非同期メソッド呼び出しを同期メソッドに同軸化しようとしています。このメソッドは、Webクライアントの完了コールバックが実行される前に戻りますので、動作しません。

Silverlightでは、非同期を採用する必要があります。これを行う1つの方法は、文字列がダウンロードされた後に実行するコードを実行する継続デリゲートを渡すことです。

関連する問題