AsyncCompletedEventArgs.Error Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan nilai yang menunjukkan kesalahan mana yang terjadi selama operasi asinkron.
public:
property Exception ^ Error { Exception ^ get(); };
public Exception Error { get; }
public Exception? Error { get; }
member this.Error : Exception
Public ReadOnly Property Error As Exception
Nilai Properti
Instans Exception , jika terjadi kesalahan selama operasi asinkron; jika tidak null
.
Contoh
Contoh kode berikut menunjukkan penggunaan AsyncOperation untuk melacak masa pakai operasi asinkron. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk System.ComponentModel.AsyncOperationManager kelas .
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
Imports System.Collections
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Drawing
Imports System.Globalization
Imports System.Threading
Imports System.Windows.Forms
// This event handler updates the ListView control when the
// PrimeNumberCalculator raises the CalculatePrimeCompleted
// event. The ListView item is updated with the appropriate
// outcome of the calculation: Canceled, Error, or result.
private void primeNumberCalculator1_CalculatePrimeCompleted(
object sender,
CalculatePrimeCompletedEventArgs e)
{
Guid taskId = (Guid)e.UserState;
if (e.Cancelled)
{
string result = "Canceled";
ListViewItem lvi = UpdateListViewItem(taskId, result);
if (lvi != null)
{
lvi.BackColor = Color.Pink;
lvi.Tag = null;
}
}
else if (e.Error != null)
{
string result = "Error";
ListViewItem lvi = UpdateListViewItem(taskId, result);
if (lvi != null)
{
lvi.BackColor = Color.Red;
lvi.ForeColor = Color.White;
lvi.Tag = null;
}
}
else
{
bool result = e.IsPrime;
ListViewItem lvi = UpdateListViewItem(
taskId,
result,
e.FirstDivisor);
if (lvi != null)
{
lvi.BackColor = Color.LightGray;
lvi.Tag = null;
}
}
}
' This event handler updates the ListView control when the
' PrimeNumberCalculator raises the CalculatePrimeCompleted
' event. The ListView item is updated with the appropriate
' outcome of the calculation: Canceled, Error, or result.
Private Sub primeNumberCalculator1_CalculatePrimeCompleted( _
ByVal sender As Object, _
ByVal e As CalculatePrimeCompletedEventArgs) _
Handles primeNumberCalculator1.CalculatePrimeCompleted
Dim taskId As Guid = CType(e.UserState, Guid)
If e.Cancelled Then
Dim result As String = "Canceled"
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, _
result)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.Pink
lvi.Tag = Nothing
End If
ElseIf e.Error IsNot Nothing Then
Dim result As String = "Error"
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, result)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.Red
lvi.ForeColor = Color.White
lvi.Tag = Nothing
End If
Else
Dim result As Boolean = e.IsPrime
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, _
result, _
e.FirstDivisor)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.LightGray
lvi.Tag = Nothing
End If
End If
End Sub
Keterangan
Jika pengecualian dinaikkan selama operasi asinkron, kelas akan menetapkan pengecualian ke Error properti . Delegasi penanganan aktivitas aplikasi klien harus memeriksa Error properti sebelum mengakses properti apa pun di kelas yang berasal dari AsyncCompletedEventArgs; jika tidak, properti akan menaikkan TargetInvocationException dengan propertinya InnerException yang memegang referensi ke Error.
Nilai Error properti adalah null
jika operasi dibatalkan.
Catatan Bagi Inheritor
Jika Anda menyediakan properti baca-saja di kelas turunan, pastikan untuk memanggil RaiseExceptionIfNecessary() metode dalam implementasi properti Anda. Ini mencegah klien mengakses properti yang berpotensi tidak valid karena kegagalan dalam operasi asinkron.