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) 方法,以便已注册的委托对事件进行接收。