2012-09-18 51 views
2

我在問XAR GamerService對話框中的應用程序的用戶,如果他真的想要刪除特定的產品。來自XNA GamerService的跨線程訪問無效

如果他按下沒錯,這會採取行動:

private void OnMessageBoxAction(IAsyncResult ar) 
     { 
      int? selectedButton = Guide.EndShowMessageBox(ar); 
      switch (selectedButton) 
      { 
       case 0: 
        WebClient cweight = new WebClient(); 
        cweight.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1"); 

        cweight.Credentials = new NetworkCredential(op.username, op.userpass); 
        cweight.DownloadStringCompleted += new DownloadStringCompletedEventHandler(deleted); 
        cweight.DownloadStringAsync(new Uri("http://mydomain.com")); 
        break; 

       case 1: 
        Debug.WriteLine("1 pressed"); 
        break; 

       default: 
        Debug.WriteLine("default pressed"); 
        break; 
      } 
     } 

,並在下載完成時,我調用登錄方法:

private void deleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      Debug.WriteLine("\n[#] deleted"); 

      if (e.Error != null) 
      { 
       Debug.WriteLine("Delete problem"); 
      } 

      Debug.WriteLine("Delete successful"); 
      login(null, null); 
     } 
login

後來我得到的無效交叉線程訪問globalprogress.Visibility = System.Windows.Visibility.Visible;,我很確定,通過整個登錄方法會發生該錯誤。

+1

'Deployment.Current.Dispatcher.BeginInvoke(()=> { 登錄(NULL,NULL); });' 確實起作用。這樣,Dispatcher將執行該代碼的一部分,而不是後臺線程。 – IMX

回答

0

手持類:

public class SmartDispatcher 
{ 
    public static void BeginInvoke(Action action) 
    { 
     if (Deployment.Current.Dispatcher.CheckAccess() 
      || DesignerProperties.IsInDesignTool) 
     { 
      action(); 
     } 
     else 
     { 
      Deployment.Current.Dispatcher.BeginInvoke(action); 
     } 
    } 
} 
相關問題