如何报告 vb.net 更改进度?

发布于 2024-07-16 16:55:26 字数 1323 浏览 5 评论 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)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-07-23 16:55:26

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

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

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
傾城如夢未必闌珊 2024-07-23 16:55:26

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文