【问题标题】:Xamarin crashes when mixing Action and API calls (threading concern?)混合操作和 API 调用时 Xamarin 崩溃(线程问题?)
【发布时间】:2018-03-30 03:19:15
【问题描述】:

我遇到了问题 - “断言崩溃” - 在 Xamarin 中混合操作和 API 调用。

MainPage.xaml.cs

private void Button_clicked(...) {
  serialLoader.Load(targetID, OnLoadSuccessful):
}

private void OnLoadSuccessful(TargetResult result) {
  // Do something
}

SerialLoader.cs

public void Load(string targetID, Action<TargetResult> OnLoadSuccessful) {
  // API service call that "forces" me to use the following
  client.LoadCompleted += (sender, e) => OnSerialLoadCompleted(sender, e, targetID, OnLoadSuccessful);
  client.LoadAsync(...) // I don't think this call is "really async" as return type is void.
}

public void OnSerialLoadCompleted(object sender, LoadCompletedEventArgs e, string targetID, Action<TargetResult> OnLoadSuccessful) {
  if (...) { // If loaded successfully...
  // .. call the Action passing the result so that I can handle it in the MainPage.xaml.cs 
    OnLoadSuccessful(e.Result);
  }
}

这会使应用程序崩溃并出现以下错误

模拟器

03-28 19:12:01.169 W/        (22852): Thread 0xcddbacd0 may have been prematurely finalized
03-28 19:12:01.169 W/        (22852): Thread 0xcddbacd0 may have been prematurely finalized
03-28 19:12:01.169 F/        (22852): * Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-6/xamarin-android/external/mono/mono/utils/mono-threads.c:563, condition `info' not met

智能手机(API 21)

03-28 21:29:27.467 E/mono-rt (28909): =================================================================
03-28 21:29:27.467 E/mono-rt (28909): Got a SIGSEGV while executing native code. This usually indicates
03-28 21:29:27.467 E/mono-rt (28909): a fatal error in the mono runtime or one of the native libraries 
03-28 21:29:27.467 E/mono-rt (28909): used by your application.
03-28 21:29:27.467 E/mono-rt (28909): =================================================================
03-28 21:29:27.467 E/mono-rt (28909): 
03-28 21:29:27.467 F/libc    (28909): Fatal signal 11 (SIGSEGV), code 2, fault addr 0x9b691fd8 in tid 29095 (Threadpool work)

我认为这与多线程上的执行有关;我尝试添加“Device.BeginInvokeOnMainThread”(没有用)并尝试用 Func 替换我的 Action,但没有运气。另外,模拟器和智能手机的崩溃如此严重让我感到惊讶!

感谢任何帮助。

其他细节: Windows 10 上的 VS2017 社区 15.6.4 Xamarin 表单 2.5.0.280555 目标 Android SDK 8.1 (API 27 - Oreo)

【问题讨论】:

    标签: c# android multithreading xamarin.forms


    【解决方案1】:

    好的,我很抱歉。上述症状是正确的,应用程序在没有提供任何反馈的情况下确实崩溃了。但问题似乎与我将 PushingAsync 到 MainPage.xaml.cs 中的 OnLoadSuccessful 方法的目标页面有关。

    为什么会发生这种情况仍然完全不清楚,但请忽略这个问题。

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多