后台工作者代表&调用者
我有一个后台工作人员执行许多 GUI 交互,但我的问题是所有这些对象都在主线程中(据我所知),并且我需要一直调用所有对象,这使得我的代码更长并且可读性较差。
1) 你知道如何创建一个调用这些 GUI 元素的通用方法吗?
2)有没有更简单的方法来面对这个问题?
提前致谢。
I have a background worker that does many GUI interactions, but my problem is that all of those objects are in the main thread (as I know), and I need to invoke all of the all the time, something that is making my code longer and less readable.
1) Do you know how can I create a generic method that will invoke those GUI elements.
2) Is there a more simple way to confront this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用BackgroundWorker,您可以ReportProgress并传递完成百分比和一个对象。您可以创建一个类来包含要传递到 UI 的数据,然后将其作为对象传递。
ProgressChanged 事件可以操作 UI 对象,因为它将在 UI 线程中运行。
您的问题示例
ReportProgress 中的第二个参数是对象类型。您可以在其中放入任何您想要的内容,然后将其转换为另一侧的正确类型。
With the BackgroundWorker you can ReportProgress and pass a percentage complete and an object. You could create a class to contain the data to pass to UI and then pass it as the object.
The ProgressChanged Event can manipulate UI Objects since it would be running in the UI Thread.
Example for your issue
The second argument in ReportProgress is an Object type. you can put whatever you want in it and cast it to the proper type on the other side.