<i id='eViyA'><tr id='eViyA'><dt id='eViyA'><q id='eViyA'><span id='eViyA'><b id='eViyA'><form id='eViyA'><ins id='eViyA'></ins><ul id='eViyA'></ul><sub id='eViyA'></sub></form><legend id='eViyA'></legend><bdo id='eViyA'><pre id='eViyA'><center id='eViyA'></center></pre></bdo></b><th id='eViyA'></th></span></q></dt></tr></i><div id='eViyA'><tfoot id='eViyA'></tfoot><dl id='eViyA'><fieldset id='eViyA'></fieldset></dl></div>
      <tfoot id='eViyA'></tfoot>
      • <bdo id='eViyA'></bdo><ul id='eViyA'></ul>
      1. <small id='eViyA'></small><noframes id='eViyA'>

        <legend id='eViyA'><style id='eViyA'><dir id='eViyA'><q id='eViyA'></q></dir></style></legend>

      2. 在异步方法中显示错误消息的更好方法

        时间:2023-10-06
          <bdo id='63kld'></bdo><ul id='63kld'></ul>
            <tbody id='63kld'></tbody>

          <small id='63kld'></small><noframes id='63kld'>

          <tfoot id='63kld'></tfoot>

          <i id='63kld'><tr id='63kld'><dt id='63kld'><q id='63kld'><span id='63kld'><b id='63kld'><form id='63kld'><ins id='63kld'></ins><ul id='63kld'></ul><sub id='63kld'></sub></form><legend id='63kld'></legend><bdo id='63kld'><pre id='63kld'><center id='63kld'></center></pre></bdo></b><th id='63kld'></th></span></q></dt></tr></i><div id='63kld'><tfoot id='63kld'></tfoot><dl id='63kld'><fieldset id='63kld'></fieldset></dl></div>

                  <legend id='63kld'><style id='63kld'><dir id='63kld'><q id='63kld'></q></dir></style></legend>
                • 本文介绍了在异步方法中显示错误消息的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们不能在 catch 块中使用 await 关键字这一事实使得在 WinRT 中显示来自异步方法的错误消息非常尴尬,因为 MessageDialog API 是异步的.理想情况下,我希望能够这样写:

                  The fact that we can't use the await keyword in catch blocks makes it quite awkward to show error messages from async methods in WinRT, since the MessageDialog API is asynchronous. Ideally I would like be able to write this:

                      private async Task DoSomethingAsync()
                      {
                          try
                          {
                              // Some code that can throw an exception
                              ...
                          }
                          catch (Exception ex)
                          {
                              var dialog = new MessageDialog("Something went wrong!");
                              await dialog.ShowAsync();
                          }
                      }
                  

                  但我必须这样写:

                      private async Task DoSomethingAsync()
                      {
                          bool error = false;
                          try
                          {
                              // Some code that can throw an exception
                              ...
                          }
                          catch (Exception ex)
                          {
                              error = true;
                          }
                  
                          if (error)
                          {
                              var dialog = new MessageDialog("Something went wrong!");
                              await dialog.ShowAsync();
                          }
                      }
                  

                  所有需要这样做的方法都必须遵循类似的模式,我真的不喜欢这种模式,因为它降低了代码的可读性.

                  All methods that need to do this have to follow a similar pattern, which I really don't like, because it reduces the code readability.

                  有没有更好的方法来处理这个问题?

                  Is there a better way to handle this?

                  我想出了这个(这类似于 svick 在他的评论中建议的):

                  I came up with this (which is similar to what svick suggested in his comments):

                  static class Async
                  {
                      public static async Task Try(Func<Task> asyncAction)
                      {
                          await asyncAction();
                      }
                  
                      public static async Task Catch<TException>(this Task task, Func<TException, Task> handleExceptionAsync, bool rethrow = false)
                          where TException : Exception
                      {
                          TException exception = null;
                          try
                          {           
                              await task;
                          }
                          catch (TException ex)
                          {
                              exception = ex;
                          }
                  
                          if (exception != null)
                          {
                              await handleExceptionAsync(exception);
                              if (rethrow)
                                  ExceptionDispatchInfo.Capture(exception).Throw();
                          }
                      }
                  }
                  

                  用法:

                  private async Task DoSomethingAsync()
                  {
                      await Async.Try(async () => 
                      {
                          // Some code that can throw an exception
                          ...
                      })
                      .Catch<Exception>(async ex =>
                      {
                          var dialog = new MessageDialog("Something went wrong!");
                          await dialog.ShowAsync();
                      });
                  }
                  

                  .Catch<...> 调用可以链接起来以模仿多个 catch 块.

                  .Catch<...> calls can be chained to mimick multiple catch blocks.

                  但我对这个解决方案并不满意;语法比以前更别扭了……

                  But I'm not really happy with this solution; the syntax is even more awkward than before...

                  推荐答案

                  C# 6 现在支持 catchfinally 中的 await,所以代码可以按照我想要的方式编写;不再需要解决方法.

                  C# 6 now supports await in catch and finally, so the code can be written the way I wanted it; a workaround is no longer needed.

                  这篇关于在异步方法中显示错误消息的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在WPF中单击菜单项时在父窗口下打开子窗口? 下一篇:Raspberry Pi 上的单声道

                  相关文章

                • <tfoot id='hwcAh'></tfoot>
                  <legend id='hwcAh'><style id='hwcAh'><dir id='hwcAh'><q id='hwcAh'></q></dir></style></legend>
                    <i id='hwcAh'><tr id='hwcAh'><dt id='hwcAh'><q id='hwcAh'><span id='hwcAh'><b id='hwcAh'><form id='hwcAh'><ins id='hwcAh'></ins><ul id='hwcAh'></ul><sub id='hwcAh'></sub></form><legend id='hwcAh'></legend><bdo id='hwcAh'><pre id='hwcAh'><center id='hwcAh'></center></pre></bdo></b><th id='hwcAh'></th></span></q></dt></tr></i><div id='hwcAh'><tfoot id='hwcAh'></tfoot><dl id='hwcAh'><fieldset id='hwcAh'></fieldset></dl></div>

                  1. <small id='hwcAh'></small><noframes id='hwcAh'>

                      • <bdo id='hwcAh'></bdo><ul id='hwcAh'></ul>