c# - 如何在 Xamarin.iOS/Mono 的任务中捕获异常?

标签 c# .net xamarin.ios

Xamarin.iOS 6.2.1、Xamarin Studio 4

我在 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 中的错误?

最佳答案

您是否尝试过在新任务委托(delegate)中包含一个 try-catch?像这样:

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

关于c# - 如何在 Xamarin.iOS/Mono 的任务中捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556586/

相关文章:

c# - 为什么 C# 不为未分配的局部变量默认为 null?

c# - 一次检索多个实体的实体请求?

c# - 如何将 lerp 转换为三次缓动?

.net - RedLaser 示例应用程序 (MonoTouch) 在加载 BarcodePickerController 时无一异常(exception)地关闭

url - 使用 Monotouch 在 UILabel 中显示可点击链接的最佳方法

c# - C# 中的类声明

c# - 如何在前台窗口获取之前捕获击键?

c# - 将 LambdaExpression 转换为带装箱返回值的类型化 Expression<Func<T, object>>

.net - 我应该从哪里下载CorFlags.exe?

c# - Xamarin.iOS 上的回声消除 (AEC)