【问题标题】:Why Xamarin not throwing Exception in another thread?为什么 Xamarin 不在另一个线程中抛出异常?
【发布时间】:2015-02-23 13:48:08
【问题描述】:

我正在使用 Xamarin 框架开发移动应用程序。 今天我尝试在另一个线程中抛出异常,如下所示:

public class ExceptionInAnotherThread
{
    public void Throw()
    {
        Task.Factory.StartNew(() =>
        {
            throw new Exception();
        });
    }
}

并在 Android 上的 MainActivity.OnCreate 和 iOS 上的 MainViewController.ViewDidLoad 中运行此代码。没有例外,应用程序运行正常。但是在控制台应用程序(Windows)中正确调用异常。有没有办法在另一个线程中抛出异常?

问候,马克斯。

【问题讨论】:

    标签: android ios multithreading exception xamarin


    【解决方案1】:

    您需要等待任务。将您的代码更改为:

    public class ExceptionInAnotherThread
    {
        public async void Throw()
        {
            await Task.Factory.StartNew(() =>
            {
                throw new Exception();
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 2012-01-11
      • 2022-01-09
      • 2014-07-09
      • 2011-11-14
      • 2010-12-09
      • 2022-09-28
      • 2011-06-25
      • 1970-01-01
      相关资源
      最近更新 更多