BackgroundWorker是AsyncOperationManager的良好替代品吗? [英] Is BackgroundWorker a good subsititute for AsyncOperationManager?

查看:164
本文介绍了BackgroundWorker是AsyncOperationManager的良好替代品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我要解决的问题:

Here's what I'm trying to solve:

我的课程(可以由UI应用程序或Windows服务或其他服务托管)需要接收Windows消息.在这里的某个地方,有人提出了建议(和一些源代码),在一个单独的线程中创建Windows窗体,该线程将创建该窗体,并且只要我感兴趣的Windows消息在WndProc上收到,它就会使用上下文触发委托. .post.

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.

我一直在尝试使它工作,但未成功.而不是在这个途径上花费更多的时间,而在尝试复制问题之前,我不得不在这里发布帮助,我想我将尝试使用BackgroundWorker来实现相同的解决方案.

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.

从我已经完成的测试中,我希望它在使用UI时能很好地工作,但是我的问题是:在不处理UI时是否有反对使用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?

按照我的设想,每当我的子"表单(在后台工作程序中运行的表单)收到消息时,我都会发出ReportProgress.我唯一需要通过线程传递的是消息ID,因此从技术上讲应该足够了吗?

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?

推荐答案

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.

这篇关于BackgroundWorker是AsyncOperationManager的良好替代品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆