溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

不要使用async void的原因是什么

發(fā)布時(shí)間:2021-08-05 11:19:53 來源:億速云 閱讀:133 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“不要使用async void的原因是什么”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“不要使用async void的原因是什么”這篇文章吧。

問題

在使用 Abp 框架的后臺作業(yè)時(shí),當(dāng)后臺作業(yè)拋出異常,會導(dǎo)致整個(gè)程序崩潰。在 Abp 框架的底層執(zhí)行后臺作業(yè)的時(shí)候,有 try/catch 語句塊用來捕獲后臺任務(wù)執(zhí)行時(shí)的異常,但是在這里沒有生效。

原始代碼如下:

public class TestAppService : ITestAppService
{
  private readonly IBackgroundJobManager _backgroundJobManager;
  public TestAppService(IBackgroundJobManager backgroundJobManager)
  {
    _backgroundJobManager = backgroundJobManager;
  }
  public Task GetInvalidOperationException()
  {
    throw new InvalidOperationException("模擬無效操作異常。");
  }
  public async Task<string> EnqueueJob()
  {
    await _backgroundJobManager.EnqueueAsync<BG, string>("測試文本。");
    return "執(zhí)行完成。";
  }
}
public class BG : BackgroundJob<string>, ITransientDependency
{
  private readonly TestAppService _testAppService;
  public BG(TestAppService testAppService)
  {
    _testAppService = testAppService;
  }
  public override async void Execute(string args)
  {
    await _testAppService.GetInvalidOperationException();
  }
}

調(diào)用接口時(shí)的效果:

不要使用async void的原因是什么

原因

出現(xiàn)這種情況是因?yàn)槿魏萎惒椒椒ǚ祷?void 時(shí),拋出的異常都會在 async void 方法啟動時(shí),處于激活狀態(tài)的同步上下文 (SynchronizationContext) 觸發(fā),我們的所有 Task 都是放在線程池執(zhí)行的。

所以在上述樣例當(dāng)中,此時(shí) AsyncVoidMethodBuilder.Create() 使用的同步上下文為 null ,這個(gè)時(shí)候 ThreadPool 就不會捕獲異常給原有線程處理,而是直接拋出。

線程池在底層使用 AsyncVoidMethodBuilder.Craete() 所拿到的同步上下文,所捕獲異常的代碼如下:

internal static void ThrowAsync(Exception exception, SynchronizationContext targetContext)
{
  var edi = ExceptionDispatchInfo.Capture(exception);
  // 同步上下文是空的,則不會做處理。
  if (targetContext != null)
  {
    try
    {
      targetContext.Post(state => ((ExceptionDispatchInfo)state).Throw(), edi);
      return;
    }
    catch (Exception postException)
    {
      edi = ExceptionDispatchInfo.Capture(new AggregateException(exception, postException));
    }
  }
}

雖然你可以通過掛載 AppDoamin.Current.UnhandledException 來監(jiān)聽異常,不過你是沒辦法從異常狀態(tài)恢復(fù)的。

解決

可以使用 AsyncBackgroundJob<TArgs> 替換掉之前的 BackgroundJob<TArgs> ,只需要實(shí)現(xiàn)它的 Task ExecuteAsync(TArgs args) 方法即可。

public class BGAsync : AsyncBackgroundJob<string>,ITransientDependency
{
  private readonly TestAppService _testAppService;
  public BGAsync(TestAppService testAppService)
  {
    _testAppService = testAppService;
  }
  protected override async Task ExecuteAsync(string args)
  {
    await _testAppService.GetInvalidOperationException();
  }
}

以上是“不要使用async void的原因是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI