BackgroundWorker 是 AsyncOperationManager 的良好替代品吗?

发布于 2024-08-19 07:01:47 字数 490 浏览 2 评论 0原文

这就是我想要解决的问题:

我的类(可以由 UI 应用程序或 Windows 服务或其他内容托管)需要接收 Windows 消息。在这里的某个地方,有人建议(和一些源代码)在一个单独的线程中创建一个 Windows 窗体,该线程将创建该窗体,并且每当我感兴趣的 Windows 消息在 WndProc 上收到时,它都会使用上下文触发委托。邮政。

我一直在努力让它发挥作用,但没有成功。在我尝试复制问题之前,我没有在该途径上花费更多时间来寻求帮助,而是想尝试使用BackgroundWorker 来实现相同的解决方案。

根据我所做的测试,我希望它在使用 UI 时工作得很好,但我的问题是:在不处理 UI 时是否有任何建议反对使用 BackgroundWorker?

编辑: 按照我的设想,每次我的“子”表单(在后台工作程序中运行的表单)收到一条消息时,我都会发出一个 ReportProgress。我唯一需要通过线程传递的是消息 ID,所以从技术上讲它应该足够了,对吧?

Here's what I'm trying to solve:

My class (which could be hosted by an UI app or a windows service or whatever), needs to receive windows messages. Somewhere around here, someone gave the suggestion (and some source code) to create a windows form in a separate thread that will create the form and whenever a windows message that I'm interested in receives on the WndProc, it triggers a delegate using context.Post.

I've been trying to make it work but unsuccessfully. Instead of spending more time on that avenue and before I try to replicate the problem I'm having there to post here for help, I'm thinking I'm going to try to implement the same solution using BackgroundWorker.

From the tests that I've done, I would expect it to work pretty good when I'm using UIs, but my question is: is there any advice against using BackgroundWorker when not dealing with UIs?

Edit:
The way I'm envisioning it, every time my "child" form (the one running in the background worker) receives a message, I will issue a ReportProgress. The only thing that I need to pass through threads is the message ID, so technically it should suffice right?

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

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

发布评论

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

评论(3

筱武穆 2024-08-26 07:01:47

BackgroundWorker和窗口是水和火。窗口需要STA线程和消息循环,BGW均不提供。检查我在此线程中的答案,以获取选择。

BackgroundWorker and a window are water and fire. A window requires an STA thread and a message loop, neither are provided by BGW. Check my answer in this thread for an alternative.

你列表最软的妹 2024-08-26 07:01:47

我想说,如果最多每 5 秒一次,那么您应该可以通过 ReportProgress 事件。

I would say if its at most every 5 seconds, then you should be fine passing the message id (as userState) back via the ReportProgress event.

影子的影子 2024-08-26 07:01:47

BackgroundWorker 对象是执行您要执行的任务的绝佳方法。但是,当您对内容进行编码时,您可能会发现简单的消息 ID 不再足够,但 BackgroundWorker.ReportProgress 方法允许您传入状态对象。如果您编写了一个高效的状态对象,您实际上可以发送回完整的快照以向父表单报告。

The BackgroundWorker object is an excellent method of performing the tasks you're looking to perform. You may, however, find that a simple message ID is no longer sufficient when you get things coded up, but the BackgroundWorker.ReportProgress method allows you to pass in a state object. If you code up an efficient state object you can literally send back thorough snapshots to report back to the parent form.

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