2013-03-21 54 views
3

Xamarin.iOS 6.2.1,Xamarin 4演播室如何在Xamarin.iOS/Mono的任務中捕獲異常?

我在ViewDidAppear()這段代碼:

this.refreshTask = Task.Factory.StartNew(() => this.RefreshContent()); 

try 
{ 
    this.refreshTask.Wait(); 
} 
catch(AggregateException aggEx) 
{ 
    aggEx.Handle(x => false); 
} 

RefreshContent()導致IndexOutOfBoundsException而訪問數組的方法。如果我直接運行該方法,我可以看到這一點。 如果我在任務中運行它,如上所述,應用程序不會失敗,我最終會得到一個空的tableview。根據這篇文章:http://msdn.microsoft.com/en-us/library/dd537614.aspx上面的代碼應該處理異常。 但是,從不觸發AggregateException

我在這裏做錯了什麼?或者它是Xamarin.iOS/Mono中的錯誤?

+0

難道這是一個線程問題? RefreshContent將在主(UI)線程上運行嗎? – 2013-03-23 20:24:35

回答

2

您是否嘗試在新任務委託中包含try-catch?像這樣:

Task.Factory.StartNew(() => { 
     try { 
      this.RefreshContent() 
     } catch (Exception ex) { 
      // Handle the exception 
     } 
    }); 
+2

這顯然有效。但是,微軟的例子呢? – Krumelur 2013-03-24 11:02:43