0

タスクを使用してUIのブロックを解除し、Aync / Awaitを使用するオプションがない場合に、エラーを処理するための推奨される方法は何ですか?

私は通常、UIを更新して予期されるエラーを処理し、Application_DispatcherUnhandledExceptionなどのグローバルエラーハンドラーで予期しないエラー(例外のログ、ユーザーへの通知、アプリのシャットダウン)を処理したいと考えています。

私は私には醜いように見える次のアプローチで遊んでいます。

Private Sub _showAsyncButton_Click(sender As Object, e As RoutedEventArgs) Handles _showAsyncButton.Click
Dim task = New WebClient().DownloadStringTaskAsync("http://www.microsoft.com")
'Dim task = New WebClient().DownloadStringTaskAsync("forcing error in async I/O")
task.ContinueWith(
    Sub()
        _resultField.Text = task.Result
        'Throw New ApplicationException("forcing error in End method")
    End Sub, Nothing, Nothing, TaskScheduler.FromCurrentSynchronizationContext
    ).ContinueWith(
        Sub(a)
            Select Case True
                Case TypeOf (a.Exception.GetBaseException) Is WebException AndAlso CType(a.Exception.GetBaseException, WebException).Status = WebExceptionStatus.NameResolutionFailure
                    'Handle expected error
                    Dispatcher.Invoke(Sub() MessageBox.Show("Cannot access web site. Please try again later"))
                Case Else
                    'Rethrow unexpected error in global error handler
                    Dispatcher.BeginInvoke(Sub() ExceptionDispatchInfo.Capture(a.Exception).Throw())
                    'ExceptionDispatchInfo.Capture(aex).Throw() 'Does not work 
            End Select
        End Sub, TaskContinuationOptions.OnlyOnFaulted)

サブ終了

4

1 に答える 1

0

Handling exceptions in a parallel application alongside handling many other stuff is a painful task and requires sufficient knowledge and skill. Consider re-writing the above code using async-await keywords introduced in .Net 4.5, it then allows you to handle exceptions in the same way you did in a synchronous programming model. Of course, there exists some situations when async-await is not adequate and you need to directly use TPL API.

You could then use a .Net decompiler (i.e. .Net Reflector) to see how much work compiler did on behalf of you, it would also be an invaluable learning source.

Also Consider taking a look at Exception Handling with the Task Parallel Library which provides you with a good insight on how to deal with exceptions when doing parallel programming using TPL.

The "The zen of async: Best practices for best performance" presented by Stephen Toub also dives deeply into how asyncworks under the hoods in .Net 4.5 and it covers some advanced topics which exception handling is one of those.

于 2012-09-01T09:04:48.617 に答える