如何报告 vb.net 更改进度?
我需要报告进展变化。 考虑以下代码:
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)
?
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)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在此处使用 BackgroundWorker 对象? 如果是这样,您要做的就是订阅calculationWorker_DoWork 事件处理程序内的CalculationProgress 事件。 您没有在 MyType 上发布任何信息,因此我假设您需要更改我的代码才能获取计算器实例。
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.
您可以在 GetCalculator 调用之后、调用Calculate 之前执行此操作。
You would do this after your GetCalculator call, and before calling Calculate.