如何让后台工作者报告的不仅仅是一个整数?
我希望后台工作程序将一些自定义信息发送回 ProgressChanged 事件处理程序,但它似乎只能发送一个 int 。
有办法解决这个问题吗?
谢谢
I want the backgroundworker to send back some custom information to the ProgressChanged event handler, but it seems that all it can send is an int.
Is there a way around this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
UserState
参数。Use the
UserState
parameter.ReportProgress 有一个重载方法,它接受一个 UserState 对象,该对象可以是任何对象。您可以传递继承对象类的任何内容,然后在 ProgressChanged 事件处理程序上解析它。
http://msdn.microsoft.com/en-us /library/system.windows.forms.toolstripprogressbar.aspx
The ReportProgress has an overloaded method which takes in a UserState object which can be any object. You can pass anything that inherits the object class in and then parse it on your ProgressChanged event handler.
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripprogressbar.aspx
我成功的另一种方法是构建我自己的具有一些额外属性的后台工作者类。在后台工作人员“DOWORK”处理程序期间,当我在做事情时,我在对象上设置其他属性... int、字符串、其他对象等。然后,以我正在处理的任何百分比发出 ReportProgress。 ..
由于后台工作程序被传递给正在侦听反馈的任何内容,因此您可以通过“get/set”读取后台工作程序上的属性值
Another approach I was successful with is by building my own Background Worker class with some extra properties. During the background worker "DOWORK" handler, as I'm doing things, I set other properties on the object... int, string, other objects, etc.. Then, issue the ReportProgress with whatever percentage I'm dealing with...
Since the background worker is passed to whatever is listening for the feedback, you can then read the values of Properties on the background worker via "get/set"