通知BackgroundWorker其他类的进度变化

发布于 2024-08-09 13:52:00 字数 309 浏览 6 评论 0原文

我想使用BackgroundWorker 或Thread 从我的Windows 窗体中调用位于业务层中的类上的方法。我希望该业务层中的方法能够在有人正在监听的情况下报告其进度,因为它可能需要一段时间才能完成。 由于我可能会先使用BackgroundWorker,然后决定使用常规线程,所以我不想与任何一个线程绑定。

如果方法不知道是否被后台工作者调用,那么报告其进度的最佳方式是什么? 我正在考虑在我的业务层类上提供一个活动,如果有人在听的话,可以发布其进度。

框架中是否已有代表?更好的是我可以实现一个接口 - 像 INotifyProgressChanged 之类的?

I want to use a BackgroundWorker or a Thread to call a method from my windows form on a class located in my business layer. I would like the method in this business layer to be able to report its progress if anyone is listening as it may take awhile to complete.
Since I may start with a BackgroundWorker and later decide to use a regular thread, I don't want to get tied to either.

What would be the best way for a method to report its progress if its unaware of the if it was called by a backgroundworker?
I was thinking of providing an event on my Business Layer Class that could publish its progress should anyone be listening.

Is there a delegate in the Framework already for that? Better Yet an Interface that I could Implement - something like INotifyProgressChanged?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

°如果伤别离去 2024-08-16 13:52:00

我相信您在触发事件处理程序方面走的是正确的方向,您可以在您的类中声明一个事件(使用 VB,C# 当然类似):

Public Event foo(ByVal progressVar1 As Double, ByVal progressVar2 As String)

然后在您的 winforms 上向其添加一些处理程序。这里出现的问题是,触发的事件将在与后台工作线程或线程相同的线程上执行,因此您仍然必须使用 Invoke() 方法来实际扰乱 UI。

I believe you are going the correct direction with firing an event handler, you can declare an event in your class (using VB, C# is similar of course) as such:

Public Event foo(ByVal progressVar1 As Double, ByVal progressVar2 As String)

then add some handlers to it on your winforms. The problem that arises here is that the fired events will execute on the same thread as thebackground worker or thread, so you'll still have to use the Invoke() method to actually mess with the UI.

浅浅 2024-08-16 13:52:00

您可以利用BackgroundWorker,这里有一些您可以订阅的事件,

   bgObj.DoWork += (sender, e)=>{ // do something in your business layer} 
   bgObj.RunWorkerCompleted += (sender, e)=>{ // do something}
   bgObj.ProgressChanged += (sender, e)=>{ // do something}      

或者,您可以在业务层中声明公共事件(实际工作),并在适当的位置引发事件,以便订阅者(例如从表单)可以获取通知。

(注意除了UI线程之外,您可能无法更新UI控件,请查看control1.BeginInvoke方法)

You could utilize BackgroundWorker, here are some events that you can subscribe,

   bgObj.DoWork += (sender, e)=>{ // do something in your business layer} 
   bgObj.RunWorkerCompleted += (sender, e)=>{ // do something}
   bgObj.ProgressChanged += (sender, e)=>{ // do something}      

Alternatively, you can declare public events within your business layer (Does the actual work), and raise the events at appropriate places so that the subscriber( say from the form ) can get the notifications.

(Note apart from UI thread, you may not be able to update the UI control, check out control1.BeginInvoke method)

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