0

我需要报告更改的进度。考虑以下代码:

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)

?

4

2 回答 2

0

您将在调用 GetCalculator 之后、调用Calculate 之前执行此操作。

于 2009-03-31T12:48:56.107 回答
0

你在这里使用BackgroundWorker对象吗?如果是这样,您要做的是订阅calculationWorker_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
于 2009-03-31T12:50:12.937 回答