BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 ProgressChanged 事件。
protected:
virtual void OnProgressChanged(System::ComponentModel::ProgressChangedEventArgs ^ e);
protected virtual void OnProgressChanged (System.ComponentModel.ProgressChangedEventArgs e);
abstract member OnProgressChanged : System.ComponentModel.ProgressChangedEventArgs -> unit
override this.OnProgressChanged : System.ComponentModel.ProgressChangedEventArgs -> unit
Protected Overridable Sub OnProgressChanged (e As ProgressChangedEventArgs)
參數
包含事件資料的 EventArgs。
範例
下列程式代碼範例示範如何使用 OnProgressChanged 方法來報告異步操作的進度。 此程式代碼範例是提供給 類別之較大範例的 AsyncOperationManager 一部分。
// This method is invoked via the AsyncOperation object,
// so it is guaranteed to be executed on the correct thread.
private void CalculateCompleted(object operationState)
{
CalculatePrimeCompletedEventArgs e =
operationState as CalculatePrimeCompletedEventArgs;
OnCalculatePrimeCompleted(e);
}
// This method is invoked via the AsyncOperation object,
// so it is guaranteed to be executed on the correct thread.
private void ReportProgress(object state)
{
ProgressChangedEventArgs e =
state as ProgressChangedEventArgs;
OnProgressChanged(e);
}
protected void OnCalculatePrimeCompleted(
CalculatePrimeCompletedEventArgs e)
{
if (CalculatePrimeCompleted != null)
{
CalculatePrimeCompleted(this, e);
}
}
protected void OnProgressChanged(ProgressChangedEventArgs e)
{
if (ProgressChanged != null)
{
ProgressChanged(e);
}
}
' This method is invoked via the AsyncOperation object,
' so it is guaranteed to be executed on the correct thread.
Private Sub CalculateCompleted(ByVal operationState As Object)
Dim e As CalculatePrimeCompletedEventArgs = operationState
OnCalculatePrimeCompleted(e)
End Sub
' This method is invoked via the AsyncOperation object,
' so it is guaranteed to be executed on the correct thread.
Private Sub ReportProgress(ByVal state As Object)
Dim e As ProgressChangedEventArgs = state
OnProgressChanged(e)
End Sub
Protected Sub OnCalculatePrimeCompleted( _
ByVal e As CalculatePrimeCompletedEventArgs)
RaiseEvent CalculatePrimeCompleted(Me, e)
End Sub
Protected Sub OnProgressChanged( _
ByVal e As ProgressChangedEventArgs)
RaiseEvent ProgressChanged(e)
End Sub
備註
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件。
OnProgressChanged 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnProgressChanged(ProgressChangedEventArgs) 時,請確定呼叫基底類別的 OnProgressChanged(ProgressChangedEventArgs) 方法,使已註冊的委派能接收到事件。