有没有办法在后台工作程序启动后将数据发送到它?
我知道您可以在第一次启动后台工作程序时通过 RunWorkerAsync 函数调用传递参数,但是您可以在其启动后向其传递数据吗?或者我是否需要创建自己的并发形式来处理从不同线程向其传递数据?
I know you can pass arguments through the RunWorkerAsync function call when you first start the backgroundworker, but can you pass it data after its already been started? Or would I need to create my own form of concurrency to handle passing data to it from a different thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要添加一些同步,并为后台工作人员提供读取数据的位置。
您无法(轻松)将数据发送到后台工作人员。如果有一个工作人员可以查找数据的地方,并且您可以添加要处理的数据,则要容易得多。只需确保在该点上进行同步,因为(至少)两个线程可能会同时访问数据。
You'll need to add some synchronization, and have a place for the background worker to read data from.
You can't (easily) SEND data to the background worker. It's much easier to just have a place where the worker can look for data, and you can just add data to process. Just make sure to put synchronization in place on that point, since (at least) two threads will be accessing the data at potentially the same time.
在任务启动后,BackgroundWorker API 中不支持传递附加数据的机制。
然而,工作例程只是在不同的线程上运行。您可以将数据传递到该线程,就像在两个任意线程之间传递数据一样。几个关于如何...的快速示例...
您应该仔细考虑这些的同步影响接近。
There is not supported mechanism within the BackgroundWorker API to pass additional data after the task has been started.
However the worker routine is simply running on a different thread. You can pass data to that thread in the same way you would pass data between 2 arbitrary threads. A couple of quick examples on how ...
You should carefully consider the synchronization impact of these approaches.
我更喜欢使用静态队列作为定期检查新消息的后台线程。这允许后台线程按照自己的节奏工作。您可以使用回调方法向主线程发出信号。正如里德所说,使用同步,例如锁定静态对象。
I prefer to use a static Queue that the background thread that periodically checks for new messages. This allows the background thread to work at its own pace. You can use callback methods to signal back to the main thread. And like Reed said, use synchronization, such as a static object to lock on.