有没有办法在后台工作程序启动后将数据发送到它?

发布于 2024-08-03 23:42:25 字数 104 浏览 6 评论 0原文

我知道您可以在第一次启动后台工作程序时通过 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 技术交流群。

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

发布评论

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

评论(3

听不够的曲调 2024-08-10 23:42:25

您需要添加一些同步,并为后台工作人员提供读取数据的位置。

您无法(轻松)将数据发送到后台工作人员。如果有一个工作人员可以查找数据的地方,并且您可以添加要处理的数据,则要容易得多。只需确保在该点上进行同步,因为(至少)两个线程可能会同时访问数据。

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.

幽梦紫曦~ 2024-08-10 23:42:25

在任务启动后,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 ...

  • State change on Static variables (probably evil)
  • State change on the object initially passed down to the worker routine (still a bit evil if not properly controlled)

You should carefully consider the synchronization impact of these approaches.

悍妇囚夫 2024-08-10 23:42:25

我更喜欢使用静态队列作为定期检查新消息的后台线程。这允许后台线程按照自己的节奏工作。您可以使用回调方法向主线程发出信号。正如里德所说,使用同步,例如锁定静态对象。

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.

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