views:

530

answers:

2

I need to report progress changed. Consider the following code:

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

*** Another 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

How and where should I subscribe to CalculationProgress event to call

 calculationWorker.ReportProgress(MyCalculationProgressEventArgs.Percent)

?

A: 

You would do this after your GetCalculator call, and before calling Calculate.

John Saunders
A: 

Are you using a BackgroundWorker object here? If so what you want to do is to subscribe to the CalculationProgress event inside of the calculationWorker_DoWork event handler. You didn't post any information on MyType, so I'll assume you'll need to alter my code to get the Calculator instance.

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
JaredPar