如何举报变更vb.net?

时间:2009-03-31 08:49:33

标签: vb.net reporting progress

我需要报告进度发生变化。请考虑以下代码:

Public Class Calculator
  Public Event CalculationProgress (ByVal sender As Object, ByVal e As MyCalculationProgressEventArgs)
Public Function Calculate(..)..
' Perform calculation here ...
' Reporting proggress
Dim args As New MyCalculationProgressEventArgs(myobj, myValue)
  RaiseEvent CalculationProgress (Me, args)
...
End Class

***另一个班级

Private WithEvents calculator As Calculator

Private Function PerformCalculation(ByVal obj As Object) As CalcParams
Dim params As CalcParams = CType(obj, CalcParams)
calculator = GetCalculator()
....
Return params.result = calculator.Calculate
End Function

Private Sub calculationWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
Handles calculationWorker.DoWork
  Dim calcResult As MyType = PerformCalculation(CType(e.Argument, MyType ))
  e.Result = calcResult
End Sub

Private Sub calculationWorker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
Handles calculationWorker.ProgressChanged
     CType(Parent.Parent, MainWindow).pbcCurrentProgress.Value = e.ProgressPercentage
End Sub

我应该如何以及在何处订阅CalculationProgress事件来调用

 calculationWorker.ReportProgress(MyCalculationProgressEventArgs.Percent)

2 个答案:

答案 0 :(得分:0)

您可以在GetCalculator调用之后和调用Calculate之前执行此操作。

答案 1 :(得分:0)

您在这里使用BackgroundWorker对象吗?如果是这样,您要做的是订阅calculateWorker_DoWork事件处理程序内的CalculationProgress事件。您没有在MyType上发布任何信息,因此我假设您需要更改我的代码以获取Calculator实例。

Private Sub calculationWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
  Handles calculationWorker.DoWork  

  Dim calcResult As MyType = PerformCalculation(CType(e.Argument, MyType ))  
  Dim calc = calcResult.Calculator
  AddHandler calc.CalculationProgress, AddressOf HandleCalculationProgress
  ...
  RemoveHandler calc.CalculationProgress, AddressOf HandleCalculationProgress
  e.Result = calcResult
End Sub